最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google analytics - javascript query string > window.location.search.substring > using # instead of ? to start quer

programmeradmin6浏览0评论

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 0
Add a ment  | 

1 Answer 1

Reset to default 3

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];
     }
   }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论