var from one HTML file to another

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

    var from one HTML file to another

    In JavaScript, how do you pass a var from one HTML file to another
    one? I "own" both of these HTML files.

    Scott
  • David Dorward

    #2
    Re: var from one HTML file to another

    Scottie wrote:
    [color=blue]
    > In JavaScript, how do you pass a var from one HTML file to another
    > one? I "own" both of these HTML files.[/color]

    Cookies, parsing the query string, frames, some server side language reading
    POST, GET or the cookies and writing a <script> element dynamically. There
    are several options.

    --
    David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>

    Comment

    • Scottie

      #3
      Re: var from one HTML file to another

      David,

      What I did was:
      var str = window.location .search;
      if (str != "") {
      var temp = str.split('=');
      alert(temp[1]);
      }
      else {
      alert("This is NULL");
      }

      and it works. Thanks.

      Scott


      David Dorward <dorward@yahoo. com> wrote in message news:<c8muck$n4 a$1$8302bc10@ne ws.demon.co.uk> ...[color=blue]
      > Scottie wrote:
      >[color=green]
      > > In JavaScript, how do you pass a var from one HTML file to another
      > > one? I "own" both of these HTML files.[/color]
      >
      > Cookies, parsing the query string, frames, some server side language reading
      > POST, GET or the cookies and writing a <script> element dynamically. There
      > are several options.[/color]

      Comment

      • Grant Wagner

        #4
        Re: var from one HTML file to another

        Scottie wrote:
        [color=blue]
        > David,
        >
        > What I did was:
        > var str = window.location .search;
        > if (str != "") {
        > var temp = str.split('=');
        > alert(temp[1]);[/color]

        What if str.split('=') doesn't create an array, or doesn't create more then one element in the array?
        What if there are multiple items being passed on the URL? You'll get both the first value and all the
        remaining text from window.location .search.

        [color=blue]
        > }
        > else {
        > alert("This is NULL");[/color]

        No, it's not null, because that's not what you're testing str against. Your condition says [if (str !=
        "") {], so the else branch of that condition is that (str == ""). In fact, if str is null, it will take
        the positive branch (because (null != ""), which will result in an error when you attempt to do
        str.split().
        [color=blue]
        > }
        >
        > and it works. Thanks.
        >
        > Scott[/color]

        Here is a much better solution:

        var s = window.location .search;
        // set s for testing
        s = 'a=b&c=d&YourAt tribute=MyValue &e=f&g=h';

        // find the exact value associated with your attribute
        (new RegExp('YourAtt ribute=([^&]+)')).test(s);
        if (RegExp.$1) {
        alert(RegExp.$1 );
        } else {
        alert('There is no value for "YourAttribute" ');
        }

        --
        | Grant Wagner <gwagner@agrico reunited.com>

        * Client-side Javascript and Netscape 4 DOM Reference available at:
        * http://devedge.netscape.com/library/...ce/frames.html
        * Internet Explorer DOM Reference available at:
        * http://msdn.microsoft.com/workshop/a...ence_entry.asp
        * Netscape 6/7 DOM Reference available at:
        * http://www.mozilla.org/docs/dom/domref/
        * Tips for upgrading JavaScript for Netscape 7 / Mozilla
        * http://www.mozilla.org/docs/web-deve...upgrade_2.html


        Comment

        Working...