javascript Javascript重新加载()不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10748270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:55:01  来源:igfitidea点击:

Javascript reload() not working

javascriptfunctionreload

提问by Peanut

I searched everywhere here to see since so many people ask this question, but no matter what, I keep getting undefined..

我在这里到处搜索,因为有这么多人问这个问题,但无论如何,我不断得到undefined..

function remove_item(itemid) {
    var window = top.location;
    var host = window.host;

    $.ajax({
        url: "http://"+host+"/backend/remove_lockbox.php?id="+itemid,
        success: function() {
            $(document).ajaxStop(function(){
                window.top.location.reload();
            });
        }
    });
}

That is my code. I tried window.location.reload, host.location.reload... I tried everything and I keep getting undefined... The parent of location is always undefinedwhether it's window, host, window.top, ANYTHING. Can someone PLEASE help me?

那是我的代码。我想window.location.reloadhost.location.reload......我什么都试过,我不断收到undefined...位置的父始终是undefined它是否是windowhostwindow.top,任何事情。有人可以帮帮我吗?

回答by epascarello

So you are doing

所以你在做

 var window = top.location;

and than you do

比你做的

 window.top.location.reload();

So you are actually saying

所以你实际上是在说

top.location.top.location.reload();

Why would you use a variable named window when that is already defined and has a different meaning? That is bad.

为什么要使用名为 window 的变量,因为它已经定义并具有不同的含义?那很不好。

If you are using frames I would expect to see something like

如果您正在使用框架,我希望看到类似

parent.location.reload(true);

or just a plain old window

或者只是一个普通的旧窗户

window.location.reload(true);

回答by Ishan Dhingra

try it this way, its working fine in chrome, as I know this should work fine in all modern browsers.

以这种方式尝试,它在 chrome 中工作正常,因为我知道这在所有现代浏览器中都可以正常工作。

function remove_item(itemid) {

    var host = window.location.host;

    $.ajax({
        url: "http://"+host+"/backend/remove_lockbox.php?id="+itemid,
        success: function() {
            $(document).ajaxStop(function(){
                window.location.reload();
            });
        }
    });
}

Here is the working example of window.location, window.location.hostand window.location.reload.

这是window.location,window.location.host和的工作示例window.location.reload

http://jsbin.com/apemen/3

http://jsbin.com/apemen/3