You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IE11中a标签target属性无法切换至已打开标签页问题求助

First, let's confirm your scenario: you're using this HTML code to try reusing the same tab for your link:

<!DOCTYPE html>
<html>
<body>
  <a href="https://www.w3schools.com" target="_yoy">click me</a>
</body>
</html>

This works perfectly in Chrome and Firefox—clicking the link reuses the tab named _yoy as expected. But in IE11, the first click opens a new tab, and subsequent clicks only highlight that tab without switching to it. This is a known compatibility bug in IE11's handling of custom target attribute values.

Solution: Control Tab Switching Manually with JavaScript

IE11 doesn't auto-switch to the highlighted tab, but it does support the window.focus() method. We can use JavaScript to maintain a reference to the opened window and force the switch:

<!DOCTYPE html>
<html>
<body>
  <a href="https://www.w3schools.com" onclick="return handleTabSwitch(this);">click me</a>

  <script>
    // Keep a reference to the target window
    let targetTab = null;

    function handleTabSwitch(linkElement) {
      // Check if the window exists and hasn't been closed
      if (targetTab && !targetTab.closed) {
        // Force switch to the existing tab
        targetTab.focus();
      } else {
        // Open a new tab and save its reference
        targetTab = window.open(linkElement.href, '_yoy');
      }
      // Prevent default link behavior to avoid duplicate tabs
      return false;
    }
  </script>
</body>
</html>

Why This Works

  • We use a global variable targetTab to keep track of the opened window. Every click checks if this window is still active.
  • If the window exists, focus() forces IE11 to switch to it—an operation the browser responds to correctly.
  • If the user closed the tab, the script opens a new one and updates the reference.
  • This solution also works seamlessly in Chrome and Firefox, so you don't lose existing functionality.

Extra Notes

  • Make sure the href of the link matches the URL used in window.open—if they differ, the existing tab will navigate to the new URL, which is standard reuse behavior.
  • For multiple custom targets, just create separate window references for each unique target name.

内容的提问来源于stack exchange,提问作者Rajib Halder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:17:44