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 %>");