SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting variables from the URL

    I got a script that gets some user input and redirects the user to the appropriate site. The problem is that the language settings for the site are in the URL (for easy linking to the right page in the right language). That Javascript redirecting function should take this variable and use it in the URL it's redirecting to, so that new page is in the right language instantly. Anybody know how to do this?

  2. #2
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    function getQueryValue(name) {
      var url = document.URL,
      i   = url.indexOf(name += '='),
      j   = url.indexOf('&', i);
      if(-1 == j) {j = url.length;}
      if(-1 != i) {return url.substring(i + name.length, j);}
    }
    
    if('yes' != getQueryValue('THIS')) {
    /* 'THIS' doesn't exist or is not equal to 'yes' */
    }
    I used this function years back... so this is a very raw version, and I haven't tested it, but it should get you your value.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks The document.URL part was invaluable! Here's my code for this now:
    Code:
    var url_lang = document.URL;
    var lang = url_lang.split("lang");
    alert(lang[1]);
    lang = lang[1].split("&");
    lang = lang[0].split("=");
    lang = lang[1];

  4. #4
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool. Glad I could help
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Better version:
    Code:
    function get_arg_val(arg) {
    	var url = document.URL;
    	if (url.match(arg) != null) {
    		var itm = url.split(arg+"=");
    		itm = itm[1].split("&");
    		itm = itm[0];
    		return itm;
    	}
    	else return false;
    }

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Much better - a reusable function to get the value of any querystring parameter.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah... there we go... I'll add that to my script libraries
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  8. #8
    SitePoint Guru mdumka's Avatar
    Join Date
    Jul 2003
    Location
    True North
    Posts
    642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree ... awsome

  9. #9
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe location.href is more standard that document.URL.
    How about this function, which creates a cache to avoid splitting the URL each time a parameter is requested. Also, if called without any parameters, it will return an object containing all the request parameters.
    Code JavaScript:
    function getParameter(key)
    {
        if (!arguments.callee.cache)
        {
            arguments.callee.cache = {};
     
            var parts = location.href.split("?");
            if (parts.length > 1)
            {
                parts = parts[1].split("&");
                for (var i = 0; i < parts.length; i++)
                {
                    var temp = parts[i].split("=");
                    arguments.callee.cache[temp[0]] = temp[1];
                }
            }
        }
     
        return (key) ? arguments.callee.cache[key] : arguments.callee.cache;
    }

  10. #10
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh wow. I quite honestly don't know why I went for the document.URL part. But I think you're right... location.href is more common, and probably implemented the same way in every client.

    This is a nice function though.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  11. #11
    SitePoint Evangelist IJoeR's Avatar
    Join Date
    Feb 2003
    Location
    Somewhere in, MD
    Posts
    400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hmm, this is confusing...

    So, lets say I have a page that has some variables in the URL and the user clicks a link or submits a form that takes them off of my site and to another site that i control and i want to pass those variables in the URL, how would i output the variable to the page with javascript?

    I see this script you guys wrote, but then how do i access the variable values -- I'm passing 4 values to a new page and i need to output them to hidden form fields...

    So, all i want to do is something like / <input type="hidden" name="utm_campaign" value="url.utm_campaign"> \

    or whatever, i don't know what the javascript command is to output url variables values.

  12. #12
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by IJoeR View Post
    So, lets say I have a page that has some variables in the URL and the user clicks a link or submits a form that takes them off of my site and to another site that i control and i want to pass those variables in the URL, how would i output the variable to the page with javascript?

    I see this script you guys wrote, but then how do i access the variable values -- I'm passing 4 values to a new page and i need to output them to hidden form fields...

    So, all i want to do is something like / <input type="hidden" name="utm_campaign" value="url.utm_campaign"> \

    or whatever, i don't know what the javascript command is to output url variables values.
    Just insert the following code somewhere below your receiving form. Change 'myForm' to the name of your form.
    The form must contain elements whose names match the names of the variables in the URL.

    Code:
    <script type='text/javascript'>
    
    function queryToForm(formRef)
    {
     var results=[], count=0;
       
     if(location.search)
     {
      qs=decodeURI(location.search.slice(1)).replace(/\+/g," ").split(/=|&/);
      
      for(var i=0, elem; i<qs.length; i+=2)
       if( elem=(formRef[qs[i]] || document.getElementByID(qs[i])) )
       {
        elem.value=qs[i+1];
        count++;
       }
     }/*2843294172747920456666656D*/
     
     return count;  
    }
    
    queryToForm( document.forms.myForm );
    
    </script>
    Last edited by Logic Ali; Feb 18, 2008 at 13:21.
    Tab-indentation is a crime against humanity.

  13. #13
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac View Post
    I believe location.href is more standard that document.URL.
    How about this function, which creates a cache to avoid splitting the URL each time a parameter is requested. Also, if called without any parameters, it will return an object containing all the request parameters.
    Code JavaScript:
    function getParameter(key)
    {
        if (!arguments.callee.cache)
        {
            arguments.callee.cache = {};
     
            var parts = location.href.split("?");
            if (parts.length > 1)
            {
                parts = parts[1].split("&");
                for (var i = 0; i < parts.length; i++)
                {
                    var temp = parts[i].split("=");
                    arguments.callee.cache[temp[0]] = temp[1];
                }
            }
        }
     
        return (key) ? arguments.callee.cache[key] : arguments.callee.cache;
    }
    Nice idea on the cache.

    Something you missed though is to watch out for escaped characters (&#37;XX). decodeURIComponent solves this nicely.

    Code javascript:
    function getParameter(key){
    	if (!arguments.callee.cache) {
    		arguments.callee.cache = {};
    		var parts = location.href.indexOf('?') >= 0 ? location.href.split('?')[1].split('&') : [];
    		for (var i = 0, l = parts.length, t; i < l; i++){
    			t = decodeURIComponent(parts[i]).split('=');
    			arguments.callee.cache[t[0]] = t[1];
    		}
    	}
    	return key ? arguments.callee.cache[key] : arguments.callee.cache;
    }

    I personally would put any querystring data in an array; like PHP.

    Code javascript:
    (function(){
    	var parts = location.href.indexOf('?') >= 0 ? location.href.split('?')[1].split('&') : [];
    	window.GET = [];
    	for (var i = 0, l = parts.length, t; i < l; i++){
    		t = decodeURIComponent(parts[i]).split('=');
    		GET[t[0]] = t[1];
    	}
    })();

    Than you could do something like alert(GET['foo']).


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
  •