点击事件触发多链接打开问题:跳转指定页同时打开新标签
The issue with your existing code is that the browser's default link navigation (triggered by the href attribute) can interfere with the window.open calls—browsers often block popups that occur alongside a main navigation, as they might be perceived as unwanted. Here's how to fix it:
Corrected Code
HTML
<a id="mybutton" href="http://example.net/quote" onclick="url_open(event)">See Offer</a>
JavaScript
function url_open(event) { // Stop the default link behavior from running immediately event.preventDefault(); // Open the two secondary URLs in new tabs window.open("http://example.net/events", "_blank"); window.open("http://example.net/gallery", "_blank"); // Navigate the current tab to the main URL window.location.href = "http://example.net/quote"; alert('Check The Tabs'); }
Why This Works
event.preventDefault(): This stops the browser from immediately navigating to thehrefURL, removing the conflict with popup calls.- Explicit Navigation: By using
window.location.hrefto navigate the current tab after opening popups, we ensure all actions are explicitly tied to the user's click event, avoiding popup blocking. - User-Initiated Actions: All
window.opencalls are directly within the click handler, so browsers recognize them as intentional user actions (not automated popups) and allow them (assuming the user hasn't blocked popups for your site).
Note: Make sure your site is allowed to open popups in the user's browser—most browsers will prompt the user the first time, so they'll need to grant permission.
内容的提问来源于stack exchange,提问作者JeVic
相关产品推荐
相关产品推荐

