-2

I need to read the url using some JS code in order to extract some parameters and execute some tasks depending on them. However I have two problems:

-The webpage is not mine so I cannot modify anything on it or inspect it in great detail. I just write some script that the client load from its webpage, and executes some functions on load. I can modify this script but not anything else.

-In this functions, I save in a variable the window.location.href value, but when doing some logs in order to verify what is reading, I see that the value does not correspond to the actual url in the address bar. Moreover, if I check the value of window.location.href in the browser console, then the value matches with the address bar!

I check this value at the start of my script and at the end and it does not match in any case. I don't know at which point of the loading does my script start execution, and I don't know what is causing this behaviour.

EDIT: Here is some code, I won't show it all as most of it works fine and doesn't have relation to my problem.

This is what I sent to my client to put in its webpage:

var s1 = document.createElement('script');
s1.src = 'source_of_my_script;
document.head.appendChild(s1);
s1.onload = function () {
    s();
};

This is part of my script:

function s(){
    var my_url_var = window.location.href;
    console.log("1st value", my_url_var);
    var param = extractParamFromUrl(my_url_var); //this functions get some value written in the url

    switch (param){
        case 'value_0':
            // do things
        case 'value_1':
            // do  other things
        defalut:
            // do default actions    
    }
    console.log("final value", window.location.href);
}

I cannot show you the exact URLs because of privacy reasons, but they are similar to these ones:

Address bar url (and what i get if I check window.location.href in browser console): https://www.client_dummy_url.com/dummy/cobranded?origin=8000097& ... (more params separated by &)

What I get from window.location.href in script: https://www.client_dummy_url.com/drfd01/main/control.do?action=start&internet=S&app=EP& ... (more params, but not all the ones I want!)

Many thanks!

jcf
  • 602
  • 1
  • 6
  • 26

1 Answers1

2

After some tries, I managed to get it to work with the solution posted by @Vaibhav at Access parent URL from iframe

//var my_url_var = window.location.href;
var my_url_var = (window.location != window.parent.location)
            ? document.referrer
            : document.location.href;
jcf
  • 602
  • 1
  • 6
  • 26