Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS window.location.href未显示与地址栏中相同的值_Javascript_Url_Location Href - Fatal编程技术网

Javascript JS window.location.href未显示与地址栏中相同的值

Javascript JS window.location.href未显示与地址栏中相同的值,javascript,url,location-href,Javascript,Url,Location Href,我需要使用一些JS代码读取url,以便提取一些参数并根据这些参数执行一些任务。然而,我有两个问题: -这个网页不是我的,所以我不能修改它上的任何东西,也不能详细检查它。我只是编写一些脚本,客户端从其网页加载,并在加载时执行一些函数。我可以修改此脚本,但不能修改任何其他内容 -在这个函数中,我将window.location.href值保存在一个变量中,但在执行一些日志以验证读取内容时,我发现该值与地址栏中的实际url不对应。此外,如果我在浏览器控制台中检查window.location.href

我需要使用一些JS代码读取url,以便提取一些参数并根据这些参数执行一些任务。然而,我有两个问题:

-这个网页不是我的,所以我不能修改它上的任何东西,也不能详细检查它。我只是编写一些脚本,客户端从其网页加载,并在加载时执行一些函数。我可以修改此脚本,但不能修改任何其他内容

-在这个函数中,我将window.location.href值保存在一个变量中,但在执行一些日志以验证读取内容时,我发现该值与地址栏中的实际url不对应。此外,如果我在浏览器控制台中检查window.location.href的值,则该值与地址栏匹配

我在脚本开始和结束时检查这个值,但它在任何情况下都不匹配。我不知道我的脚本在哪个加载点开始执行,也不知道是什么导致了这种行为

编辑:这里有一些代码,我不会全部显示,因为大部分代码都很好,与我的问题无关

这是我发送给我的客户并放在其网页中的内容:

var s1 = document.createElement('script');
s1.src = 'source_of_my_script;
document.head.appendChild(s1);
s1.onload = function () {
    s();
};
这是我脚本的一部分:

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);
}
由于隐私原因,我无法向您显示确切的URL,但它们与以下类似:

地址栏url(以及在浏览器控制台中检查window.location.href时得到的内容):。。。(更多参数由&分隔)

从脚本中的window.location.href获取的内容:。。。(更多参数,但不是我想要的所有参数!)


非常感谢

尝试了几次之后,我设法让它与@Vaibhav在发布的解决方案一起工作


在iframe中,您可以控制运行的脚本在哪里?请向我们展示一些代码。
//var my_url_var = window.location.href;
var my_url_var = (window.location != window.parent.location)
            ? document.referrer
            : document.location.href;