2016-04-30 81 views
0

因此,我創建了一個無用的網站,我需要製作一個按鈕,將用戶指向新標籤中的隨機網站。我已經有了隨機鏈接按鈕代碼,但我似乎不能用新的標籤代碼添加它。如何在新標籤頁中隨機鏈接中打開HTML按鈕

樣品

var randomlinks=new Array(10) 

randomlinks[0]="http://ducksarethebest.com/" 
randomlinks[1]="http://cant-not-tweet-this.com/" 
randomlinks[2]="http://just-shower-thoughts.tumblr.com/" 
randomlinks[3]="http://www.fallingfalling.com/" 
randomlinks[4]="http://www.partridgegetslucky.com/" 
randomlinks[5]="http://ducksarethebest.com/" 
randomlinks[6]="http://cant-not-tweet-this.com/" 
randomlinks[7]="http://just-shower-thoughts.tumblr.com/" 
randomlinks[8]="http://www.fallingfalling.com/" 
randomlinks[9]="http://www.staggeringbeauty.com/" 
randomlinks[10]="http://www.trypap.com/" 

function randomlink(){ 
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)] 
} 

的Html

<form method="post"> 
    <p><input type="button" name="B1" value="Random Link >>" onclick="randomlink()""window.open'"></p> 
</form> 
    or 
    <a href="javascript:randomlink()">Random Link</a> 

回答

0

您需要使用window.open(url),而不是window.location(url)

window.location(url)表示:設置當前選項卡的網址。

編號:https://developer.mozilla.org/en-US/docs/Web/API/Window/open

實例:(不計算器原因沙盒框上工作)

var randomlinks = []; 
 
randomlinks[0]="http://ducksarethebest.com/"; 
 
randomlinks[1]="http://cant-not-tweet-this.com/"; 
 
randomlinks[2]="http://just-shower-thoughts.tumblr.com/"; 
 
randomlinks[3]="http://www.fallingfalling.com/"; 
 
randomlinks[4]="http://www.partridgegetslucky.com/"; 
 
randomlinks[5]="http://ducksarethebest.com/"; 
 
randomlinks[6]="http://cant-not-tweet-this.com/"; 
 
randomlinks[7]="http://just-shower-thoughts.tumblr.com/"; 
 
randomlinks[8]="http://www.fallingfalling.com/"; 
 
randomlinks[9]="http://www.staggeringbeauty.com/"; 
 
randomlinks[10]="http://www.trypap.com/"; 
 

 
function randomlink(){ 
 
    window.open(randomlinks[Math.floor(Math.random()*randomlinks.length)]); 
 
}
<form method="post"> 
 
    <p><input type="button" name="B1" value="Random Link >>" onclick="randomlink()"></p> 
 
</form> 
 
    or 
 
<a href="#" onclick="randomlink()">Random Link</a>

+0

所以,你能告訴我完整的代碼我應該使用和添加。 –

+0

@IshraqKhan更新。 – SEUH

相關問題