Created
May 17, 2012 15:49
-
-
Save endigo9740/2719725 to your computer and use it in GitHub Desktop.
Javascript: Fetch URL Parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// URL Parameters | |
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; | |
} | |
getUrlVars()['PARAM_NAME_HERE']; | |
// ===== V2 - Source: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values === | |
// URL Parameters v2 | |
function getParameter(name) | |
{ | |
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); | |
var regexS = "[\\?&]" + name + "=([^&#]*)"; | |
var regex = new RegExp(regexS); | |
var results = regex.exec(window.location.search); | |
if(results == null) | |
return ""; | |
else | |
return decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
var foobar = getParameter("PARAM_NAME_HERE"); | |
// ===== Regex ===== | |
// Custom Regex Solution | |
var url = window.location.search; | |
var wapURL = url.match("wap[=](.*)[&]nonwap"); // WAP URL | |
console.log(wapURL[1]); | |
var nonwapURL = url.match("[&]nonwap[=](.*)"); // NON-WAP URL | |
console.log(nonwapURL[1]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment