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 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    query strings in javascript

    how do i retrieve parameters in html from a link such as: http://mysite.com/?param=1
    does anyone know how to retrieve that information in javascript?

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function getE(){
           	var url_string = String(document.location);
           	var url_array1 = url_string.split('?');
    		if(url_array1[1]){
    			var e_array = url_array1[1].split('&');
    			for(i=0;i<=(e_array.length);i++){
    				if(e_array[i]){
    					var useVal = e_array[i].split('=');
    					alert(useVal[0]+"  ==  "+useVal[1]);// output
    				}
    			} 
    		}
    }
    edit:

    ok, this loops through the url pulling all variables (useVal[0]) and all values corresponding to those variables (useVal[1]).

    Remember though, it does this in the order in which they appear.

    so in the example:

    /editpost.php?do=editpost&p=316287

    in order it would alert:

    do == editpost
    p == 316287
    Last edited by rlemon; 06-02-2005 at 08:43 PM.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #3
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    using rlemon's code...

    Code:
    	function getE(){
           	var url_string = String(document.location);
           	var url_array1 = url_string.split('?');
    		var tObject = new Object();
    		if(url_array1[1]){
    			var e_array = url_array1[1].split('&');
    			for(i=0;i<=(e_array.length);i++){
    				if(e_array[i]){
    					var useVal = e_array[i].split('=');
    					if ( useVal.length == 2 ) {
    						tObject[useVal[0].toLowerCase()] = useVal[1];
    					}
    				}
    			} 
    		}
    		return tObject;
    	}
    If you call
    qStr = getE();

    You can get from the string;
    /editpost.php?do=editpost&p=316287
    qStr['do'] = editpost;
    qStr['p'] = 316287

    But
    qStr['Do'] = undefined

  • #4
    New Coder
    Join Date
    Mar 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how do i display whats after the ?param=

  • #5
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    are you not looking at the example?

    ok, here's a tip.

    if you are returning text, you use innerHTML with a span tag.

    this is done as follows:

    Code:
    function getE(){
           	var url_string = window.location.href;
           	var url_array1 = url_string.split('?');
    		if(url_array1[1]){
    			var e_array = url_array1[1].split('&');
    			for(i=0;i<=(e_array.length);i++){
    				if(e_array[i]){
    					var useVal = e_array[i].split('=');
    					if(useVal[0] == "param"){
    						var output = document.getElementById('span_id');
    						output.innerHTML = useVal[1];
    					}
    				}
    			} 
    		}
    }
    then in the html

    Code:
    <p>
    blah blah blah this is some text, hey you want to know what param is?<br />
    Why yes, yes i do want to know!<br />
    well, param = <span id="span_id"></span>!<br />
    YAY!
    </p>
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Glenn
    ____________________________________

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


  •  

    Posting Permissions

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