2017-05-24 9 views
1

私はリクエストがあります:新しいタブでリンクを開くためのJavaScriptボタン

私はisbnの本をAmazonの本へリンクするアプリを持っています。私は一度に50〜100のisbnsで貼り付けて、Amazonとその激しい本で見ている各リンクをクリックする必要があります。

誰かが、ウィンドウの新しいタブにすべてのisbnリンクを開くボタンを実装するのに役立つでしょうか?あなたは文字列としてのURLをお持ちの場合は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

:私が持っていることができれば誰かが

ここ

がJsfiddleコードです)=私はそれが私の手を救う唯一の1回のクリックでこれを行うボタンを得るのを助けます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

神様、私はあなたを愛して!あなたは最高です! :) –

関連する問題