2017-05-24 151 views
1

我有一個請求:Javascript按鈕打開新標籤頁中的鏈接

我有一個應用程序,它可以將書籍鏈接到亞馬遜書上的鏈接。我一次會粘貼50-100個isbns,我必須點擊每個鏈接才能查看關於Amazon及其繁重的書籍。

有人可以幫助我實現一個按鈕,它打開窗口中新選項卡中的所有isbn鏈接?如果我能有一個人幫助我獲得了一個按鈕,只用1次點擊它會拯救我的手做這個=)

這裏是的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

如果您有URL作爲一個字符串在JavaScript中,將其傳遞到window.open,它將在新標籤中打開。您可以循環播放,並根據需要多次播放。

下面是您的代碼,進行一些小修改:存儲url的數組以及點擊時會將它們全部打開到新窗口中的按鈕。 (注意: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 = [] 
 
\t 
 
//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"/>

+0

上帝,我愛你!你是最棒的! :) –

相關問題