2015-02-08 38 views
0

我试图每秒钟在服务器上调用一个页面,只有当用户所在的当前页面是Create页面或Edit页面时。但此代码不起作用:检查window.location.href.indexOf不起作用

function sessionRefresh(){ 
var remoteURL = '/path/to/file'; 
    $.get(remoteURL, function(data){ 
     setTimeout('sessionRefresh()',1000); 
    }); 
} 

if(window.location.href.indexOf("/create" != -1) || window.location.href.indexOf("/edit" != -1)) { 
    setTimeout('sessionRefresh()',1000); 
} 

sessionRefresh()函数正在所有页面上运行。但我只希望它运行,如果其中有URL /create/edit。需要注意的是,我不打算在生产服务器上每秒获取一次该文件。它只是用于测试。

我该如何重写这个函数才能在那些创建和编辑页面上调用?

回答

2

你犯了复制粘贴错字,试

if(window.location.href.indexOf("/create") != -1 || window.location.href.indexOf("/edit") != -1) { 

,而不是

if(window.location.href.indexOf("/create" != -1) || window.location.href.indexOf("/edit" != -1)) { 
+1

哎呀!我不能相信这是一个错字。我的编辑(dw)没给我语法警告。感谢Nathan – 2015-02-08 13:36:49

-1

更换

if(window.location.href.indexOf("/create" != -1) || window.location.href.indexOf("/edit" != -1)) { 
setTimeout('sessionRefresh()',1000); 

}

if(window.location.href.indexOf(“/ create”)!= -1 || window.location.href.indexOf(“/ edit”)!= -1)setTimeout('sessionRefresh()',1000); }

+0

现有答案有什么区别? – 2015-02-08 13:23:22