Global Variable In A Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SamMan

    Global Variable In A Function

    I have a .js file that receives a value from an html page. I'd like this
    value to be a global variable that all functions in the .js file can use. I
    have tried just declaring a var at the top of my .js file, but when the
    value comes into the function and gets assigned to that variable, it is not
    global.

    Any help, hints, etc. would be appreciated.

    Thanks,
    Sam


  • Jérôme VUIBERT

    #2
    Re: Global Variable In A Function

    Strange it works for me but the global variable is in the <script> part of
    the html document.

    jerome

    "SamMan" <psf@psfdev.com > a écrit dans le message de news:
    FieNa.186802$jT 4.3319378@twist er.rdc-kc.rr.com...[color=blue]
    > I have a .js file that receives a value from an html page. I'd like this
    > value to be a global variable that all functions in the .js file can use.[/color]
    I[color=blue]
    > have tried just declaring a var at the top of my .js file, but when the
    > value comes into the function and gets assigned to that variable, it is[/color]
    not[color=blue]
    > global.
    >
    > Any help, hints, etc. would be appreciated.
    >
    > Thanks,
    > Sam
    >
    >[/color]


    Comment

    • SamMan

      #3
      Re: Global Variable In A Function

      What I'm trying to do is to pass a value from "first.html " to a function in
      the .js file (works fine). This value should get assigned to a global
      variable (not working) and within the same function, a new window is opened.
      "second.htm l". This second pop-up window then calls (onLoad) another
      function within the same .js file and writes the value of the global
      variable to the page.

      Thanks,
      Sam

      "Jérôme VUIBERT" <jerome.vuibert @fleximage.fr> wrote in message
      news:3f05724d$0 $5401$626a54ce@ news.free.fr...[color=blue]
      > Strange it works for me but the global variable is in the <script> part of
      > the html document.
      >
      > jerome
      >[/color]


      Comment

      • Martin Honnen

        #4
        Re: Global Variable In A Function



        SamMan wrote:[color=blue]
        > I have a .js file that receives a value from an html page. I'd like this
        > value to be a global variable that all functions in the .js file can use. I
        > have tried just declaring a var at the top of my .js file, but when the
        > value comes into the function and gets assigned to that variable, it is not
        > global.[/color]

        If you declare
        var varName;
        outside of any function then you have declared a global variable which
        you can then use as
        varName = expression



        --

        Martin Honnen


        Comment

        • SamMan

          #5
          Re: Global Variable In A Function

          "Martin Honnen" <Martin.Honnen@ t-online.de> wrote in message
          news:3F057882.3 090206@t-online.de...[color=blue]
          >
          >
          > If you declare
          > var varName;
          > outside of any function then you have declared a global variable which
          > you can then use as
          > varName = expression
          >
          >[/color]

          Thanks Martin,

          I'm aware of what you are saying, but the problem I'm having is that unless
          I initialize the var (var varName = "someValue" ), the variable remains null,
          or undefined. In the code below, when "text" is assigned to "print", it is
          not global, and "print" retains the value of "null".

          Thanks for the help,
          Sam

          var print="null";

          function testWindow(text ) { //called by first page
          print = text;
          alert("first -" + print)
          var newWin;
          newWin = window.open("da taPage.html","" , "height=80,widt h=250");
          }

          function passVar() { //called by pop-up page
          alert("second function -" + print);
          document.write( print);
          }


          Comment

          Working...