I'm trying to create a button that links to random websites like on 'The useless web'. I found this code on here and it's worked almost perfectly. I just need help making the sites open in a new tab, at the moment they change the current window. Any help will be greatly appreciated. THANKS
CURRENT:
<script type="text/javascript">
var urls = [
"http://www.kittenwar.com/",
'http://heeeeeeeey.com/',
'http://cat-bounce.com/'
];
function goSomewhere() {
var url = urls[Math.floor(Math.random()*urls.length)];
window.location = url; // redirect
}
onClick handler assigned:
<a href="#" onClick="goSomewhere(); return false;">Gimme something weird!</a>
use this function
function goSomewhere() {
var url = urls[Math.floor(Math.random()*urls.length)];
window.open(url, '_blank')
}
To open a page in a new tab, you should create the button like this:
<button href='w3schools.com' target='_blank'>Click me!</button>
You can change the href
tag with JavaScript.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.