javascript query string > window.location.search.substring > using # instead of ? to start query string
Just get the hash
value instead of the search
value from the location
object
Here is a working example.
function GetURLParameter(sParam)
{
var sPageURL = window.location.hash.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
Rowe Morehouse
Rowe Morehouse | @rowemore | http://git.io/rowemore Growth · Product Management · Frontend Dev · Design · Technical Writing · Sales
Updated on June 05, 2022Comments
-
Rowe Morehouse almost 2 years
I'm trying to extract hash substring values from window.location and write them into HTML for the user to see, with an URL syntax that disallows use of
?
as query string delimiter.Ok, so I have this great code example, thanks to @Gabe:
<html> <input type="button" id="test" value="Test" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> <script> $(function() { $('#test').click(function() { window.location = (window.location); GetURLParameter('source'); }); }); function GetURLParameter(sParam) { var sPageURL = window.location.hash.substring(1); console.log(sPageURL); var sURLVariables = sPageURL.split('&'); for (var i = 0; i < sURLVariables.length; i++) { var sParameterName = sURLVariables[i].split('='); if (sParameterName[0] == sParam) { alert(sParameterName[1]); return sParameterName[1]; } } } </script> </html>
I made one modifcatin to his example, so we're using the actual window location from the client ::
window.location = (window.location);
When I hit the "test" submit button on a page containing the above code (the URL is formed like this):
http://example.com/pagename.html#source=FOO&medium=BAR&campaign=FRED
(notice no
?
as query string delimiter, just#
)... then I get a successful alert, that shows the source "FOO" from the URL. Awesome.
But how to I get the other two SParamaterNames, and then how do I write them into the page as HTML rather than return an alert.
I know this is a basic question for you guys. I'm here because I have been trying to solve this problem for days now, looking at many diff solutions. Any help is greatly appreciated!! thank you very much to all you wizards.
-
Rowe Morehouse over 11 yearsthanks, did not work for me. I updated my code example after your answer.
-
Gabe over 11 yearsI don't see that you made an attempt in your update to use the hash. Clearly it works with my jsfiddle example.
-
Rowe Morehouse over 11 yearsthanks Gabe ... will look at your fiddle more carefully. I tried implementing your solution on my server, but did not work. Will try again.
-
Rowe Morehouse over 11 yearsOK @gabe, modified your fiddle example slightly. Any help here with my clarified question would make you a god in my eyes. :)
-
Dimitri Bostrovich about 4 yearsI am kind of late to the party, but how do you get the second variable out of the query string in the fiddle example? I can only seem to get the first one