仅当用户所在的当前页面是创建
页面或编辑
页面时,我每秒都会尝试调用服务器上的页面。但这段代码不起作用:
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
时运行。请注意,我不会在生产服务器上每秒获取此文件。它只是用于测试。
如何重写此函数,以便仅在那些创建和编辑页面上调用该函数?
最佳答案
您复制粘贴错误,请尝试
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)) {
关于javascript - 检查 window.location.href.indexOf 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394087/