我有使用超链接在亚马逊上打开书籍ISBN的Javascript代码(请随时尝试,例如: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>
&#13;
答案 0 :(得分:0)
不,这是不可能的。网页无法决定是否在标签页或新窗口中打开内容。由用户/浏览器决定。
最好的情况是,您可以使用window.open()
打开一个新窗口,但这并不能让您稍后控制特定窗口中的标签。