Hossein Shahsahebi
Hossein Shahsahebi

Reputation: 7288

location.href return strange URL

I'm using location.href to get the full URL exists in browser address bar.
To provide more detail, it's important to note that our service has a js file that will be included in our customers sites. So this JS file will generate the full URL of applicant.

I thought this URL is somehow previous URL that redirected to real domain, but how I should prevent this action?

The line of JS code that will generate a link for iframe's src attribute is:

'http://sd.domain.ir/index.php?r=' + Math.floor(Math.random() * (100000000 - 0 + 1)) + 0 + '&ref=' + ((window.btoa) ? btoa(location.href) : 'ie') + '&responsive=' + ((document.querySelector('meta[name="viewport"][content*="width=device-width"]')) ? 'yes' : 'no') + '&params='

Examples of applicant UA:
Mozilla\/5.0 (Linux; U; Android 4.3; en-us; HUAWEI G620-L72 Build\/HuaweiG620-L72) AppleWebKit\/534.24 (KHTML, like Gecko) Version\/4.0 Mobile Safari\/534.24 T5\/2.0 bdbrowser\/6.1.0.4

Mozilla\/5.0 (Linux; U; Android 4.4.3; en-ae; HTC_One Build\/KTU84L) AppleWebKit\/534.30 (KHTML, like Gecko) Version\/4.0 Mobile Safari\/534.30

Mozilla\/5.0 (Linux; U; Android 4.3; en-us; GT-I9300 Build\/JSS15J) AppleWebKit\/534.30 (KHTML, like Gecko) Version\/4.0 Mobile Safari\/534.30

...

sometimes strange url generated by location.href and I don't know what is the reason. For example:

Main URL is something like below: http://saten.ir/tag/%D8%A8%DB%8C%D9%88%DA%AF%D8%B1%D8%A7%D9%81%DB%8C-%D8%A7%D9%85%DB%8C%D8%B1%D8%B9%D8%A8%D8%A7%D8%B3-%D9%81%D8%AE%D8%B1%D8%A2%D9%88%D8%B1/

But the URL returned by location.href is as below:
http://www.google.com/search?site=&source=hp&ei=mpkeWIvHKYWbsgGtxaSQBg&q=%D8%A7%D9%85%D9%8A%D8%B1%D8%B9%D8%A8%D8%A7%D8%B3+%D9%81%D8%AE%D8%B1%D8%A7%D9%88%D8%B1&oq=%D8%A7%D9%85%D9%8A%D8%B1%D8%B9%D8%A8%D8%A7%D8%B3+%D9%81%D8%AE%D8%B1%D8%A7%D9%88%D8%B1&gs_l=mobile-gws-hp.3...4752.15339.0.16567.0.0.0.0.0.0.0.0..0.0....0...1c.1.64.mobile-gws-hp..0.0.0.UFWL1tf4KDM#scso=uid_WB6ZrwAGHJ0KLAXLjA8j8w_10:2120,uid_WB6aQQAGpZkKLNwFPgmnbA_10:2120

Upvotes: 9

Views: 1356

Answers (2)

Kesty
Kesty

Reputation: 479

I wasn't actually able to reproduce the bug, in all the few testing I did your code got the correct location.href.

The only problem that I can think of that could do that is that you are creating your link when you are building your class.

var my_obj = {
     /*...*/
     url: "http://sd.domain.ir/index.php?r=" + Math.floor(Math.random() * (1e8 + 1)) + "0&ref=" + (window.btoa ? btoa(location.href) : "ie") + "&responsive=" + (document.querySelector('meta[name="viewport"][content*="width=device-width"]') ? "yes" : "no") + "&params=",
     /*...*/
}
my_obj.init();

Doing it like this means that the url is actually populated when the .js file is loaded.

What you get it is definitely a referrer, like if in old versions of the android browser you actually load the script before location.href changes, or there is some sort of prefetching (like the browser load stuff from the next page before actually going to the next page) going on.

Getting location.href after the dom has loaded should be better. You could try to check what state you are in before getting the href:

var my_obj = {
     init: function(){
            my_obj.url =  "http://sd.domain.ir/index.php?r=" +Math.floor(Math.random() * (1e8 + 1)) + "0&ref=" + (window.btoa ? btoa(location.href) : "ie") + "&responsive=" + (document.querySelector('meta[name="viewport"][content*="width=device-width"]') ? "yes" : "no") + "&params=";
     }
     /*...*/,
     url: ""
}

if(document.readyState == "uninitialized" || document.readyState == "loading "  || document.readyState == "loaded"){
    document.onreadystatechange = function(){
        if (document.readyState == "interactive") {
            my_obj.init();
         }
    } 
}else{
   my_obj.init();
}

Upvotes: 0

Emiliano
Emiliano

Reputation: 728

try do this

var myurl = location.origin + decodeURIComponent(location.pathname);

basically is the same that do location.href but it should works

Upvotes: 2

Related Questions