Javascript Switch

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis M. Marks

    Javascript Switch

    I have a javascript that displays the site contents and it is included
    in every one of my pages. It has logic that makes it work a little
    differently in the home page.

    Currently I check the page title and if it the home page title I do one
    thing otherwise I do another. I wanted to add a translator for the page
    which changes the page title.

    I would like to get away from using the page title as a check because
    of this. I have tried placing a global variable in a separate script on
    the home page and it works fine for the home page. The problem is that
    it is undefined on the other pages.

    Is there a way to check for the presence of a variable without giving
    an error if not there? I don't want to define it on every page unless I
    have to.

    BTW: If I have a script like this: <script src="xxx"
    type="text/javascript">som e code</script>. Will "some code" be
    processed before source code, after source code, or not at all. Is it
    consistant on all browsers?

    --
    Dennis M. Marks

    Replace domain.invalid with dcsi.net


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
  • Mike

    #2
    Re: Javascript Switch

    > Is there a way to check for the presence of a variable without giving[color=blue]
    > an error if not there? I don't want to define it on every page unless I
    > have to.[/color]

    Use the typeof operator to check and see if your variable is defined.

    example:

    if (typeof(myVar) != 'undefined'){
    //do processing
    }

    [color=blue]
    >
    > BTW: If I have a script like this: <script src="xxx"
    > type="text/javascript">som e code</script>. Will "some code" be
    > processed before source code, after source code, or not at all. Is it
    > consistant on all browsers?[/color]

    All browsers as far as I know parse the document in top down order. So if
    you want some script to execute before the HTML source is parsed simply
    place the script block above the <html> tag.

    Regards
    Mike


    Comment

    • Cenekemoi

      #3
      Re: Javascript Switch

      Bonjour à Mike <nospam@please. com> qui nous a écrit :[color=blue][color=green]
      >> Is there a way to check for the presence of a variable without giving
      >> an error if not there? I don't want to define it on every page
      >> unless I have to.[/color]
      >
      > Use the typeof operator to check and see if your variable is defined.
      >
      > example:
      >
      > if (typeof(myVar) != 'undefined'){
      > //do processing
      > }[/color]

      Better (IMHO) :

      if (typeof(window['myVar') != 'undefined'){
      //do processing
      }

      --
      Cordialement, Thierry ;-)

      Comment

      • Michael Winter

        #4
        Re: Javascript Switch

        On Thu, 04 Mar 2004 07:09:47 -0800, Dennis M. Marks
        <denmarks@domai n.invalid> wrote:

        [snip]
        [color=blue]
        > Is there a way to check for the presence of a variable without
        > giving an error if not there? I don't want to define it on every
        > page unless I have to.[/color]

        function isDefined( ref ) {
        return 'undefined' != typeof ref;
        }
        ...
        if( isDefined( aVariable )) {
        ...
        }
        [color=blue]
        > BTW: If I have a script like this: <script src="xxx"
        > type="text/javascript">som e code</script>. Will "some code" be
        > processed before source code, after source code, or not at all. Is
        > it consistant on all browsers?[/color]

        If you specify the source attribute, there should be nothing within the
        element. From the HTML 4.01 Specification, Section 18.2.1 - The SCRIPT
        element:

        "If the src attribute is not set, user agents must interpret the
        contents of the element as the script. If the src has a URI value,
        user agents must ignore the element’s contents and retrieve the
        script via the URI."

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Randy Webb

          #5
          Re: Javascript Switch

          Dennis M. Marks wrote:[color=blue]
          > I have a javascript that displays the site contents and it is included
          > in every one of my pages. It has logic that makes it work a little
          > differently in the home page.
          >
          > Currently I check the page title and if it the home page title I do one
          > thing otherwise I do another. I wanted to add a translator for the page
          > which changes the page title.
          >
          > I would like to get away from using the page title as a check because
          > of this. I have tried placing a global variable in a separate script on
          > the home page and it works fine for the home page. The problem is that
          > it is undefined on the other pages.[/color]

          Check window/document.locati on.href and the title won't matter.

          [color=blue]
          > Is there a way to check for the presence of a variable without giving
          > an error if not there? I don't want to define it on every page unless I
          > have to.
          >
          > BTW: If I have a script like this: <script src="xxx"
          > type="text/javascript">som e code</script>. Will "some code" be
          > processed before source code, after source code, or not at all. Is it
          > consistant on all browsers?[/color]

          Not at all unless the browser doesn't understand the src attribute.


          --
          Randy
          Chance Favors The Prepared Mind
          comp.lang.javas cript FAQ - http://jibbering.com/faq/

          Comment

          • Dennis M. Marks

            #6
            Re: Javascript Switch

            In article <c27ipc$v3$1@ne ws-reader2.wanadoo .fr>, Cenekemoi
            <tbaudessonENLE VER@harrysoftwa re.ENVELERcom> wrote:
            [color=blue]
            > Bonjour à Mike <nospam@please. com> qui nous a écrit :[color=green][color=darkred]
            > >> Is there a way to check for the presence of a variable without giving
            > >> an error if not there? I don't want to define it on every page
            > >> unless I have to.[/color]
            > >
            > > Use the typeof operator to check and see if your variable is defined.
            > >
            > > example:
            > >
            > > if (typeof(myVar) != 'undefined'){
            > > //do processing
            > > }[/color]
            >
            > Better (IMHO) :
            >
            > if (typeof(window['myVar') != 'undefined'){
            > //do processing
            > }[/color]
            Thank both of you for your help but I couldn't do what I wanted to
            anyway.

            --
            Dennis M. Marks

            Replace domain.invalid with dcsi.net


            -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
            http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
            -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Javascript Switch

              Cenekemoi wrote:
              [color=blue]
              > if (typeof(window['myVar') != 'undefined'){[/color]
              ^^^^^^^^^^^^^^^ ^[color=blue]
              > //do processing
              > }[/color]

              Since "typeof" is an operator and not a method, the parantheses
              are not required, (white)space suffices. Besides, there is a
              nesting error in the code, it cannot work this way.


              PointedEars

              Comment

              Working...