在后台打开一个新标签,并保留当前标签

时间:2018-08-07 16:54:44

标签: javascript html

在我开始说话之前,我在stackoverflow中尝试了related post,但是它不再起作用了,您将看到您是否阅读了注释或我无法应用它。

我的页面中有链接,我想在后台的新标签页中打开并保持当前标签页。这是你的代码

所以我的问题可能了吗?在确定之前,我已经看过了,我想让它变得如此。

window.onload = function() {
  var puURL = 'http://example.com';
  var puTS = Math.round(+new Date() / 1000);
  if (typeof localStorage.puTS == 'undefined' || parseInt(localStorage.puTS) <= (puTS - 3600)) {
    var links = document.getElementsByTagName('a');
    for (var i = 0, len = links.length; i < len; i++) {
      links[i].onclick = function(e) {
        var puHref = this.getAttribute("href");
        var puTarget = this.getAttribute("target");
        if (puHref !== '#' && puHref !== 'javascript:void(0)') {
          // e.preventDefault();    
          if (puTarget == 'blank') {
            window.open(window.location.href);
          }
          window.open(puHref);
          window.location.href = puURL;
          localStorage.puTS = puTS;
        }
      }
    }
  }
};
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
</head>

<body>
  <a href="http://www.fontawesome.com">fontawesome</a>
  <br>
  <a href="https://getbootstrap.com">Bootstrap</a>
  <br>
  <a href="https://www.stackoverflow.com">stackoverflow.com</a>
</body>

</html>

0 个答案:

没有答案