2016-04-30 24 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)は、現在のタブのURLを設定することを意味します。

参考:https://developer.mozilla.org/en-US/docs/Web/API/Window/open

例:(stackoverflowの原因サンドボックスフレーム上では動作しません)

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

関連する問題