简体   繁体   中英

How to trigger <a>'s click event on safari

http://nycweb.io/test.html

Its html is

<a href="http://www.google.com" target="_blank" style="display:block">click</a>

In chrome's console, if you do

document.getElementsByTagName('a')[0].click()

It will open a new page of google.com. Surprisingly, in Safari, this doesn't work. I searched around and found this page saying there is a bug with Mobile Safari which prevents the click event from being triggered. Also it lists some workarounds. I was hoping it would help when I started reading it, but it actually makes me more confused.

First of all, it says the bug only happens with Mobile Safari, but anyone with a Macbook can test from the link above that it doesn't work with Safari either; second of all, it says it only happens with "elements that aren't typically interative", and the workaround includes adding "href" to <a> to make it interative. But my test above shows that it doesn't work with <a> at all, no matter it has href or not.

The actual problem I am trying to solve is this page " http://fbnydob.applinzi.com/test.php ", where you can see a warning message pushed by the Hosting company, which can only be prevented by click the little 'x'. I was trying to click it programmatically but it is an <a> even without href , so my program fails to work on Apple devices and my users keep seeing this unpleasant message.

Any workaround for this that actually works?

Could you try firing the click event?
document.getElementsByTagName('a')[0].fire('click') This will call the click handler and close the popup window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM