admin管理员组文章数量:1332198
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=".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):
.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.
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./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./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.
Share Improve this question edited Aug 29, 2012 at 21:19 Rowe Morehouse asked Aug 22, 2012 at 23:00 Rowe MorehouseRowe Morehouse 4,5853 gold badges30 silver badges30 bronze badges 01 Answer
Reset to default 3Just 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];
}
}
}
本文标签:
版权声明:本文标题:google analytics - javascript query string > window.location.search.substring > using # instead of ? to start quer 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742243603a2438989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论