在 Chrome 中设置 location.href 两次
我用来
location.href=document.getElementById("link1").href;
location.href=document.getElementById("link2").href;
运行与链接关联的脚本:
<a id="link1" href="javascript:...">
<a id="link2" href="javascript:...">
代码在 Firefox 中运行良好,但在 Chrome 中仅执行最后一个 location.href 命令(因此仅执行第二段 javascript 代码)。如何强制它执行第一次和第二次?
I'm using
location.href=document.getElementById("link1").href;
location.href=document.getElementById("link2").href;
to run the scripts associated to the links:
<a id="link1" href="javascript:...">
<a id="link2" href="javascript:...">
The code works fine in Firefox, but in Chrome only the last location.href command (and so only the second piece of javascript code) is executed. How can I force it to execute both the first time and the second one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有类似的情况。我通过使用 setTimeout 函数稍微延迟第二个函数来解决这个问题,并且效果很好。
I had a similar situation. I solved it by using
setTimeout
function to delay the second one a bit and it works fine.尝试将 JavaScript 代码放入函数中。
然后,您可以在单击链接时从链接中调用这些函数,也可以在任何其他需要的地方调用它们。
这是一种比使用 url 更稳定的运行代码方式。
Try putting your javascript code into functions.
You can then call those functions from the links when they are clicked, as well as anywhere else you need them.
This is a much more stable way of running code rather than with urls.