问题
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