Fun w/Query Strings & Object Prototypes in jQuery
HTML
<h1>Fun w/Query Strings</h1>
<p>To see results, turn on your Chrome/Safari inspector console, and/or light up FireBug on FirFox</p>
JavaScript
$.getUrlHash = function(uhash ){
uhash = (uhash || window.location.href)
results = uhash.match(/#(.*?)\?/i);
if(results && results.length > 0) return decodeURIComponent(results[1]);
};
String.prototype.getUrlHash = function() {
return $.getUrlHash(this);
};
$.getUrlParam = function(queryStringArg, url){
url = (url || window.location.href);
var results = new RegExp('[\\?&]' + queryStringArg + '=([^&#]*)').exec(url);
if(results && results.length > 0) return decodeURIComponent(results[1]);
};
String.prototype.getUrlParam = function(queryStringArg) {
return $.getUrlParam(queryStringArg, this);
};
myHash = "#section-feed?secid=sports";
myHash = window.location.href + myHash;
myTest = myHash.getUrlHash();
console.log("test getUrlHash() = ", myTest);
myTest = myHash.getUrlParam("secid");
console.log("test getUrlParam('secid') = ", myTest);