setting location.href twice in chrome

眉间皱痕 提交于 2020-01-03 05:16:08

问题


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?


回答1:


I had a similar situation. I solved it by using setTimeout function to delay the second one a bit and it works fine.




回答2:


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.



来源:https://stackoverflow.com/questions/7787788/setting-location-href-twice-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!