Javascript按钮在新标签页中打开链接

时间:2017-05-24 22:22:42

标签: javascript

我有一个请求:

我有一个应用程序,可以将书籍isbn's转入亚马逊书籍的链接。我一次只能粘贴50-100个isbns,我必须点击每个链接才能在亚马逊上查看该书及其费力。

有人可以帮我实现一个按钮,在窗口的新标签页中打开所有isbn链接吗?如果我可以让别人帮我一个按钮,只需点击一下就可以保存我的手=)

以下是Jsfiddle代码:https://jsfiddle.net/ks51zch8/

<html>
<head>

</head>
<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.com/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((?:[a-z0-9A-Z]\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);
      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>

1 个答案:

答案 0 :(得分:1)

如果您在javascript中将网址作为字符串,请将其传递到window.open,然后它会在新标签中打开。您可以根据需要循环并多次执行。

以下是您的代码,其中包含一些小修改:一个存储网址的数组,以及一个在点击时将它们全部打开到新窗口的按钮。 (注意:snippit不起作用,因为它不允许弹出窗口)

var input = document.getElementById('numbers');
var button = document.getElementById('button');
var output = document.getElementById('output')
var base = 
    'https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords='

var urls = []
	
//adding an event listener for change on the input box
input.addEventListener('input', handler, false);
button.addEventListener('click', openAllUrls, false);

//function that runs when the change event is emitted
function handler () {
  var items = input.value.split(/\b((?:[a-z0-9A-Z]\s*?){10,13})\b/gm);
  urls=[];
  // 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);
      container.appendChild(link);
      urls.push(base + item);//add the url to our array of urls for button click
    } else { // it is the text next to the matches
      container.appendChild(document.createTextNode(item))
    }
  });
  // Replace output
  output.innerHTML = '';
  output.appendChild(container);
}
function openAllUrls(){
  for(var i=0; i< urls.length; i++){//loop through urls and open in new windows
    window.open(urls[i]);
  }
}
handler(); // run on load
<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>
<input type="button" id="button" Value="Open All"/>