2016-04-30 4 views
0

Also ich eine nutzlose Website erstellen und ich muss eine Schaltfläche, die Menschen auf zufällige Websites in neue Registerkarten leitet. Ich habe bereits den Random-Link-Button-Code, aber ich kann nicht scheinen, es mit dem neuen Tab-Code hinzuzufügen.Wie mache ich eine HTML-Schaltfläche in zufälligen Links in der neuen Registerkarte öffnen

Probe

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> 

Antwort

0

Sie müssen window.open(url) statt window.location(url) verwenden.

window.location(url) bedeutet: Einstellung der URL der aktuellen Registerkarte.

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

Beispiel: (funktioniert nicht auf Stackoverflow Ursache Sandbox Rahmen)

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

So können Sie mir den vollständigen Code sagen, ich benutze und fügen sollte. –

+0

@ IshraqKhan aktualisiert. – SEUH

Verwandte Themen