JavaScript代码在Chrome和IE上运行正常,但在Firefox上无法正常工作。

4
我正在尝试运行一些小的JavaScript代码。我需要通过某个链接刷新页面上的数据,然后使用更新后的数据加载该页面。 由于我使用的平台限制,我必须在打开页面之前打开一个链接以更新数据。 我快速编写了一些JavaScript并进行了测试。它在Internet Explorer和Google Chrome中完美运行,但由于某种原因,在Firefox中似乎无法运行while循环。我不确定为什么它不起作用,因为我查找了语法并且似乎是正确的。
var refreshed = false;
while (refreshed != true) {
  refreshpage = window.open('https://na10.salesforce.com/dash/dashboardRefresh');
  refreshed = true;
  refreshpage.close();
}
window.open('/dashboard', '__tab');

我尝试使用超时函数来关闭窗口。但我不确定是否使用正确。它会执行所有内容-包括循环中的内容-但从未关闭窗口。

以下是更新后的代码:

var refreshed = false;
var timeOut = setTimeout(function() {
  refreshpage.close();
}, 1000);

while (refreshed != true) {
  refreshpage = window.open('https://na10.salesforce.com/dash/dashboardRefresh');
  refreshed = true;
  clearTimeout(timeOut);
}
window.open('/dashboard', '__tab');

1
似乎它没有运行。使用调试器,查看实际运行的代码。 - Halcyon
请注意,您没有给新页面足够的时间来加载。window.open只是启动了该过程,然后您立即通过关闭窗口取消了它。为了确保请求得到可靠地发送,更不用说得到响应,您需要等待一段时间。 - T.J. Crowder
我正在使用Salesforce,因此onclick是由该平台生成的。我不认为这是问题,因为它将执行循环外的代码。但出于某种原因,它无法执行循环中的代码。 - user1072453
T.J. 我尝试使用超时方法稍微延迟一下,但这完全没有帮助。 - user1072453
1
如果您的问题已经解决,应该将解决方案发布为答案并将其标记为“已接受”。 - Bali Balo
显示剩余5条评论
1个回答

0

原始帖子内容:

Here is what I used to get it to work:

var refreshed = false;
while (refreshed != true) {
  refreshpage = window.open('https://na10.salesforce.com/dash/dashboardRefresh');
  refreshed = true;
  var timeOut = setTimeout(function() {
    refreshpage.close();
    window.open('/dashboard', '__tab');
  }, 500);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接