这在浏览器中可能吗?[JavaScript / HTML]

Is This Possible in a Browser? [JavaScript / HTML]

本文关键字:JavaScript HTML 浏览器      更新时间:2023-09-26

我有Javascript代码,可以使用超链接打开Amazon上图书的isbn号(请随意尝试,例如:0133098648)。我想在URL打开一个新的窗口与所有链接点击在同一窗口上的新选项卡。似乎只能在当前窗口的新选项卡中打开,或者每次打开一个新窗口。

我想做的是可能的吗?我一直在阅读,像这样的东西是受浏览器的安全原因限制;也许还有别的办法?我一直在绞尽脑汁想办法解决这个问题,如果我能的话,这会让我的生活更轻松。

用图片来描述我的问题:http://imgur.com/a/5lUP4

请使用JSfiddle示例:http://jsfiddle.net/mq1efed2(代码不能在Stackoveflow上工作)

<html>
<div><b>ISBN Hyperlinker</b></div>
<textarea id=numbers placeholder="paste isbn numbers as csv here" style="width:100%" rows="8" >
</textarea>
<div><b>Hyperlinked text:</b></div>
<div id="output" style="white-space: pre"></div>
<script>
//the input box.
var input = document.getElementById('numbers');
var output = document.getElementById('output')
var base = 
    'https://www.amazon.ca/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords='
//adding an event listener for change on the input box
input.addEventListener('input', handler, false);
//function that runs when the change event is emitted
function handler () {
  var items = input.value.split(/'b((?:'d's*?){10,13})'b/gm);
  // Build DOM for output
  var container = document.createElement('span');
  items.map(function (item, index) {
    if (index % 2) { // it is the part that matches the split regex:
      var link = document.createElement('a');
      link.textContent = item.trim();
      link.setAttribute('target', '_blank');
      link.setAttribute('href', base + item.replace(/'D+/g, ''));
      container.appendChild(link);
    } else { // it is the text next to the matches
      container.appendChild(document.createTextNode(item))
    }
  });
  // Replace output
  output.innerHTML = '';
  output.appendChild(container);
}
handler(); // run on load
</script>
</html>

不,这不可能。网页不能决定内容是在选项卡中打开还是在新窗口中打开。这是由用户/浏览器决定的。

您最多可以使用window.open()打开一个新窗口,但这并不能让您稍后在特定窗口中控制选项卡。