Access GET variables using jQuery/Javascript

Go To StackoverFlow.com

2

I know that this question has been asked before, but I have had trouble with all of the solutions posted so far. I have a url that looks like this:

http://localhost:3000/virgil/1/render_lesson?toggle=view

It is a Rails call through AJAX, but that should not matter. Everytime I try a function that is supposed to get the variable for "toggle" I get "undefined" instead when I print it to the consoles log. I have tried this function

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

And when I call console.log(getUrlVars()); I get ["http://localhost:3000/"]

But when I call console.log(getUrlVars()["toggle"]); I simply get undefined

I appreciate any help!

EDIT:

Adding the relevant parts of my code. This is a Rails application, that makes the call using AJAX.

<%= link_to 'View Lessons', render_lesson_virgil_path(course, :toggle => :view), :remote => true, :class => 'toggle_lesson', :id => "toggle_lesson#{course.id}" %>

And then in the javascript file that it calls:

function $_GET( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

console.log($_GET('toggle')); //This is where it prints the incorrect value to the log, regardless of the function I use.

$("#Course<%= @course.id %>").append('<div id="Lesson<%= @course.id %>" class="render_lesson"></div>');
$("#Lesson<%= @course.id %>").html("<%= escape_javascript(render( :partial => "lesson" )).html_safe  %>");
2012-04-03 21:32
by Rhawb
possible duplicate of how to get GET and POST variables with JQuery?Bozho 2012-04-03 21:34
and http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquer - Bozho 2012-04-03 21:34
I changed your function to accept a URL string instead of grabbing it from window.location. I pushed in your URL as a string and it worked for me; I got an array that had a "toggle" key and "view" value - SuperJumbo 2012-04-03 22:14


3

This is the JS function I use for query strings - hopefully it helps you.

function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
2012-04-03 21:34
by mikevoermans
This appears to have the same issue. When I call console.log(getParameterByName('toggle')); I get null instead of the proper value - Rhawb 2012-04-03 21:52
It works for me - Karl Henselin 2013-07-06 14:31


0

Try this (i only changed line 4 - "var hashes ..."):

function getUrlVars()
{
    var vars = [], hash;    
    var hashes = window.location.href.valueOf().split('?').slice(1)[0].split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
2012-04-03 21:39
by Kasapo


-2

I am using it in my 1 of Projects..

function $_GET( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

looks like PHP $_GET['']... because i love PHP :D

Usage Example:

URL: index.php?page=newone&id=4&cat=15

Javascript: alert($_GET('page')); // newone

2012-04-03 21:56
by Shahrukh
Its weird when I print that to console.log I get (an empty string)Rhawb 2012-04-03 23:48
@Rhawb: i have tested it again now.. it is working for me.. can you show me your code.. that you are using.. - Shahrukh 2012-04-03 23:57
I have posted my code on the original posting. Thank you - Rhawb 2012-04-03 23:59
use console.log($_GET('toggle')); instead of console.log(getUrlVars()["toggle"]);.. And try again. - Shahrukh 2012-04-04 00:06
I am using that call. You have too look at the end of my original post past my edit, that is the code that I am now using. Sorry if that wasn't clear - Rhawb 2012-04-04 00:09