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

function - Javascript reload() not working - Stack Overflow

programmeradmin1浏览0评论

I searched everywhere here to see since so many people ask this question, but no matter what, I keep getting 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 undefined whether it's window, host, window.top, ANYTHING. Can someone PLEASE help me?

I searched everywhere here to see since so many people ask this question, but no matter what, I keep getting 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 undefined whether it's window, host, window.top, ANYTHING. Can someone PLEASE help me?

Share Improve this question edited May 25, 2012 at 3:58 Derek 朕會功夫 94.3k45 gold badges195 silver badges253 bronze badges asked May 25, 2012 at 3:53 PeanutPeanut 3,2957 gold badges29 silver badges47 bronze badges 6
  • 1 Nothing wrong with window.top.location.reload();, but really? var window = top.location;? It does not seem right. – Derek 朕會功夫 Commented May 25, 2012 at 3:55
  • another way window.top.location = window.top.location – Nhu Trinh Commented May 25, 2012 at 3:57
  • use document.location = document.location – Imdad Commented May 25, 2012 at 3:58
  • 2 What is "undefined"? HUH? You should not be using a variable named window! – epascarello Commented May 25, 2012 at 4:08
  • 1 Derek was right. I had to erase the window = variable i set in the very beginning. – Peanut Commented May 25, 2012 at 4:10
 |  Show 1 more comment

2 Answers 2

Reset to default 14

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.

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);

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

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.host and window.location.reload.

http://jsbin.com/apemen/3

发布评论

评论列表(0)

  1. 暂无评论