SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member a_youngers's Avatar
    Join Date
    Sep 2003
    Location
    Seattle
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing Variables Using Javascript

    How do you pass varibles from one html page to another using javascript?

    A

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Specify the relation of these two pages. Is one a popup (i.e., both open at the same time). Or is the 2nd page linked to from the first, and uses the same window?

    Either way, there are a couple ways to pass data between windows

    1) Use cookies
    2) Use the query-string
    3) Access variables directly (if both windows are open, only)
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Member a_youngers's Avatar
    Join Date
    Sep 2003
    Location
    Seattle
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The variables will be passed into a pop up window that will be loaded from a hyperlink a page that is already loaded. How do you pass variable using the query-string. The information isn't sensitive.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two ways to use the query string

    1) a hidden GET-method form
    2) Create your own query string and append it to the URL before opening the popup.

    Any inkling on which you'd prefer?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Member a_youngers's Avatar
    Join Date
    Sep 2003
    Location
    Seattle
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a hidden GET-method form? sounds good. Also how to do you retrieve the variables once you have passed them.

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use my Get Class to retrieve and parse the query string.

    Oh, and here's a quick little object I wrote for making query strings
    Code:
    function QueryString()
    {
    	this.qs = "";
    	this.vars = new Array();
    
    	this.add = function( name, value )
    	{
    		this.vars[this.vars.length] = escape( name ) + "=" + escape( value );
    	}
    
    	this.generate = function()
    	{
    		return "?" + this.vars.join( "&" );
    	}
    }
    And sample usage
    Code:
    function openWin( url )
    {
    	var qs = new QueryString();
    	qs.add( "hello", "world" );
    	window.open( url + qs.generate() );
    }
    [img]images/smilies/biggrin.gif[/img]
    Last edited by beetle; Sep 19, 2003 at 10:34.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Member a_youngers's Avatar
    Join Date
    Sep 2003
    Location
    Seattle
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I think I follow your code, but I'm still unsure of how you capture it on the recieveing popup page. Lets say I simply want to write it to the page. What do I do? The following is what I have set up and I'm trying to call popup.htm in the same location.

    <html>
    <head>
    <title>Untitled Document</title>
    <script language="JavaScript">
    function QueryString()
    {
    this.qs = "";
    this.vars = new Array();
    this.add = function( name, value )
    {
    this.vars[this.vars.length] = name + "=" + value;
    }
    this.generate = function()
    {
    return "?" + this.vars.join( "&" );
    }
    }
    function openWin( url )
    {
    var qs = new QueryString();
    qs.add( "hello", "world" );
    window.open( url + qs.generate() );
    }
    </script>
    </head>
    <body>
    <a href="javascriptpenWin(popup.htm)">test link</a>
    </body>
    </html>

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to use this code (or something similar) in the receiving page, popup.htm. I have a test page for it here, (try it with the query-string)
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

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