Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing a variable (a number) from one web page to another

    Hi, I would like to pass some numbers (which, of course, change from time to time) from one web page to another. For example, let's say I have some memory variables in PAGE1 called:

    var numb1=100
    var numb2= 200
    var numb3=300

    I would like to call those numbers up in a new page called PAGE2. How would I call up these memory variable from PAGE1?

    I realize the answer is incredibly simple, but I've been searching for the past 2 days and haven't found the answer.

    Thanks for your help

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    function go(){
      var numb1=100;
      var numb2= 200;
      var numb3=300;
      location.href = "page2.htm?n1=" + numb1 + "&n2=" + numb2 + "&n3=" + numb3;
    }
    html:
    <input type="button" value="Go to Page 2" onclick="go()">

    then use this script to retrieve the querystrings n1, n2, n3 from page 2.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Dec 2003
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks Glenngv

    Thanks for taking the time to respond. I REALLY appreciate it. It was so frustrating trying to find this answer.

  • #4
    New Coder
    Join Date
    Dec 2003
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What are the names of the variables?

    I put the code on page1 and page2 as instructed. As you can see, the mem variables were transfered to the second page via the location:

    file:///M:/Web%20counter/page2.htm?n1=100&n2=200&n3=300

    However, when I get to the second page, I am unable to find the variables after they had been extracted. I had assumed the variable names were going to be:

    n1
    n2
    n3

    However, they don't seem to work. Am I looking for the wrong varialbe names? If I wanted to display the 3 variables that were transfered and extracted, how would I do that. Maybe that would give me a clue as to what I'm doing wrong.

    Thanks again for your help.

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You may find this helpful.

    .....Willy

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Judging from the term "variables" you used to call the extracted data from the url, I suspect that you expect n1, n2 and n3 to be variables that you can use directly in your new page. No, they are not. They are called querystring parameters and you can extract and put them in variables. For basic functionality, here are the codes you only need:
    Code:
    function GetVars( def ) 
    {
        this._def_ = def;
        var query, queries = top.location.search.substring(1).split( /\&/ );
        for ( var i=0; (query = queries[ i ]); i++ )
        {
            query = query.split( /\=/ );
            this[query[0]] = ( typeof query[1] == 'undefined' ) ? def : unescape(query[1]).replace( /\+/g, " " );
        }
    }
    
    GetVars.prototype.exists = function( key )
    {
        return ( typeof this[key] != 'undefined' );
    }
    
    GetVars.prototype.assign = function( key )
    {
        return ( this.exists( key ) ) ? this[key] : this._def_;
    }
    Then to access the querystring parameters:
    Code:
    var _GET = new GetVars( '' );
    var num1 = _GET.assign( 'n1' );
    var num2 = _GET.assign( 'n2' );
    var num3 = _GET.assign( 'n3' );
    Now you can use the variables num1, num2, num3 (not n1, n2 and n3) in your codes.

    Or for a simpler code (but same method), you can use the code in the link posted by Willy Duitt. But it should be the first topic which you were exactly asking for (not the window.name trick) since on some circumstances (special characters in window.name value, named window, etc) mentioned by the author, that trick does not work.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    New Coder
    Join Date
    Dec 2003
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I am still having problems extracting the data

    Thanks Glenn for your help. Sometimes I think that my other programing experience is more of a hinderence than a help. Maybe the way I'm trying to verify that the data was pass is incorrect. Maybe there is a better way to verify it. Anyhow, following is what I had done:

    I inserted the coding for the function GetVar. Also, (right below the GetVar function) I inserted the coding to access the query string. However, I am having problems verifying that it was passed. To test it I wrote the following:

    document.write(num1);

    Nothing was displayed. Any ideas of what i am doing wrong? Should I try to verify it some other way. Since my knowledge of Javascript is limited, I'm having to learn many of the simple programming tricks all over again.

  • #8
    New Coder
    Join Date
    Dec 2003
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Continuation of the previous post

    This is a continuation of the previous post. I thought I would show you how I constructed the coding itself in case that was the cause of the problem. Here it is:



    <script type="text/javascript" language="javascript">

    function GetVars( def )
    {
    this._def_ = def;
    var query, queries = top.location.search.substring(1).split( /\&/ );
    for ( var i=0; (query = queries[ i ]); i++ )
    {
    query = query.split( /\=/ );
    this[query[0]] = ( typeof query[1] == 'undefined' ) ? def : unescape(query[1]).replace( /\+/g, " " );
    }
    }

    GetVars.prototype.exists = function( key )
    {
    return ( typeof this[key] != 'undefined' );
    }

    GetVars.prototype.assign = function( key )
    {
    return ( this.exists( key ) ) ? this[key] : this._def_;
    }







    var _GET = new GetVars( '' );
    var num1 = _GET.assign( 'n1' );
    var num2 = _GET.assign( 'n2' );
    var num3 = _GET.assign( 'n3' );






    document.write(num1);


    </script>

  • #9
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not sure if this help its something I made sort of like glen's

    http://www.the-hive.dk/~donp/Library/QueryTest.html

    Note if you use please don't direct link to the js copy it to your own page

  • #10
    New Coder
    Join Date
    Dec 2003
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks guys, I got it to work

    Sorry for the problems guys, but I did finally get it to work. I'm not sure what happened because I repeated everything I had tried before, but it is working this time.

    Thanks

  • #11
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    then use this script to retrieve the querystrings n1, n2, n3 from page 2
    There are problems with that script that cause it to incorrectly parse certain query strings. An alternative is availble. The following discussion to which also details several of the flaws present in other such scripts.
    Last edited by Jeff Mott; 01-11-2004 at 04:12 AM.

  • #12
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's funny u mention only this method,that's because there are other methods too.
    i.e. U can use:
    var mywin=window.open() //To open the new window
    Then u can:
    opener.myvar
    //...To access a variable
    opener.myMethod()
    //...To access a method


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •