2016-04-30 83 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

相关问题