admin管理员组文章数量:1026444
I have some code that'll allow me to open up a random website from a list of websites but I'd like to open each one up in a new tab, how do I do this?
Information you may need
the code
<html>
<button onclick="randomLink();OpenInNewTab();">Click here to go somewhere else! </button>
<script type="text/javascript">
var randomLink = function () {
// first create an array of links
var links = [
"bbc",
"google",
"youtube",
"facebook"
];
// then work out the maximum random number size
// by counting the number of links in the array
var max = (links.length)
// now generate a random number
var randomNumber = Math.floor(Math.random()*max);
// use that random number to retrieve a link from the array
var link = links[randomNumber];
// change the location of the window object
window.location = "http://" + link;
// Opens a new tab.
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
}
</script>
</html>
I tried doing the action in question at two different points and hope you're input can help to correct this.
Location 1
<button onclick="randomLink();OpenInNewTab();">Click here to go somewhere else!
Location 2
// Opens a new tab.
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
The following web address is what the code currently looks like and does.
- /
*EDIT : The only changes I made are the websites.They're much more on the live demo.
I have some code that'll allow me to open up a random website from a list of websites but I'd like to open each one up in a new tab, how do I do this?
Information you may need
the code
<html>
<button onclick="randomLink();OpenInNewTab();">Click here to go somewhere else! </button>
<script type="text/javascript">
var randomLink = function () {
// first create an array of links
var links = [
"bbc.",
"google.",
"youtube.",
"facebook."
];
// then work out the maximum random number size
// by counting the number of links in the array
var max = (links.length)
// now generate a random number
var randomNumber = Math.floor(Math.random()*max);
// use that random number to retrieve a link from the array
var link = links[randomNumber];
// change the location of the window object
window.location = "http://" + link;
// Opens a new tab.
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
}
</script>
</html>
I tried doing the action in question at two different points and hope you're input can help to correct this.
Location 1
<button onclick="randomLink();OpenInNewTab();">Click here to go somewhere else!
Location 2
// Opens a new tab.
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
The following web address is what the code currently looks like and does.
- http://holyshitthisisalotofuselesscraponotherwebsites./
*EDIT : The only changes I made are the websites.They're much more on the live demo.
Share Improve this question edited Jul 1, 2014 at 13:17 Konstantin V. Salikhov 4,6532 gold badges37 silver badges48 bronze badges asked Jul 1, 2014 at 11:40 flyinggoatmanflyinggoatman 431 silver badge3 bronze badges 4- check this stackoverflow./questions/4907843/… – ashok_p Commented Jul 1, 2014 at 11:50
- call OpenInNewTab() at the end of the randomLink() function (from inside the function). It is within the scope of that funtion. – EthanK Commented Jul 1, 2014 at 11:51
- Please could you display the changes for me? because I tried and it wouldn't work. @Ethan – flyinggoatman Commented Jul 1, 2014 at 13:09
- @ashok_p I already tried and it just did not work :( – flyinggoatman Commented Jul 1, 2014 at 13:19
2 Answers
Reset to default 2The code you wrote is wrong, since you change the address of the current window (via the line window.location=...
, and other issues... but here:
Working example fiddle
Very similar, and works.
Code
HTML
<button onclick="openStuff();">Click here to go somewhere else!</button>
JS
// the used links
var links = [
"bbc.",
"google.",
"youtube.",
"facebook."];
openStuff = function () {
// get a random number between 0 and the number of links
var randIdx = Math.random() * links.length;
// round it, so it can be used as array index
randIdx = parseInt(randIdx, 10);
// construct the link to be opened
var link = 'http://' + links[randIdx];
// open it in a new window / tab (depends on browser setting)
window.open(link);
};
Based on Matyas' answer, I updated the example to work in strict mode. Working Example on Codepen.
Code
HTML
<button id="open-link">Click here to go somewhere else!</button>
JS
"use strict";
// the used links
const links = ["bbc.", "google.", "youtube.", "facebook."];
const openLink = function (links) {
// get a random number between 0 and the number of links
// and round it, so it can be used as array index
const randIdx = parseInt(Math.random() * links.length, 10);
// construct the link to be opened
const link = "https://" + links[randIdx];
// open it in a new window or tab (depends on browser setting)
window.open(link);
};
window.onload = function () {
document.getElementById("open-link").onclick = () => openLink(links);
};
I have some code that'll allow me to open up a random website from a list of websites but I'd like to open each one up in a new tab, how do I do this?
Information you may need
the code
<html>
<button onclick="randomLink();OpenInNewTab();">Click here to go somewhere else! </button>
<script type="text/javascript">
var randomLink = function () {
// first create an array of links
var links = [
"bbc",
"google",
"youtube",
"facebook"
];
// then work out the maximum random number size
// by counting the number of links in the array
var max = (links.length)
// now generate a random number
var randomNumber = Math.floor(Math.random()*max);
// use that random number to retrieve a link from the array
var link = links[randomNumber];
// change the location of the window object
window.location = "http://" + link;
// Opens a new tab.
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
}
</script>
</html>
I tried doing the action in question at two different points and hope you're input can help to correct this.
Location 1
<button onclick="randomLink();OpenInNewTab();">Click here to go somewhere else!
Location 2
// Opens a new tab.
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
The following web address is what the code currently looks like and does.
- /
*EDIT : The only changes I made are the websites.They're much more on the live demo.
I have some code that'll allow me to open up a random website from a list of websites but I'd like to open each one up in a new tab, how do I do this?
Information you may need
the code
<html>
<button onclick="randomLink();OpenInNewTab();">Click here to go somewhere else! </button>
<script type="text/javascript">
var randomLink = function () {
// first create an array of links
var links = [
"bbc.",
"google.",
"youtube.",
"facebook."
];
// then work out the maximum random number size
// by counting the number of links in the array
var max = (links.length)
// now generate a random number
var randomNumber = Math.floor(Math.random()*max);
// use that random number to retrieve a link from the array
var link = links[randomNumber];
// change the location of the window object
window.location = "http://" + link;
// Opens a new tab.
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
}
</script>
</html>
I tried doing the action in question at two different points and hope you're input can help to correct this.
Location 1
<button onclick="randomLink();OpenInNewTab();">Click here to go somewhere else!
Location 2
// Opens a new tab.
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
The following web address is what the code currently looks like and does.
- http://holyshitthisisalotofuselesscraponotherwebsites./
*EDIT : The only changes I made are the websites.They're much more on the live demo.
Share Improve this question edited Jul 1, 2014 at 13:17 Konstantin V. Salikhov 4,6532 gold badges37 silver badges48 bronze badges asked Jul 1, 2014 at 11:40 flyinggoatmanflyinggoatman 431 silver badge3 bronze badges 4- check this stackoverflow./questions/4907843/… – ashok_p Commented Jul 1, 2014 at 11:50
- call OpenInNewTab() at the end of the randomLink() function (from inside the function). It is within the scope of that funtion. – EthanK Commented Jul 1, 2014 at 11:51
- Please could you display the changes for me? because I tried and it wouldn't work. @Ethan – flyinggoatman Commented Jul 1, 2014 at 13:09
- @ashok_p I already tried and it just did not work :( – flyinggoatman Commented Jul 1, 2014 at 13:19
2 Answers
Reset to default 2The code you wrote is wrong, since you change the address of the current window (via the line window.location=...
, and other issues... but here:
Working example fiddle
Very similar, and works.
Code
HTML
<button onclick="openStuff();">Click here to go somewhere else!</button>
JS
// the used links
var links = [
"bbc.",
"google.",
"youtube.",
"facebook."];
openStuff = function () {
// get a random number between 0 and the number of links
var randIdx = Math.random() * links.length;
// round it, so it can be used as array index
randIdx = parseInt(randIdx, 10);
// construct the link to be opened
var link = 'http://' + links[randIdx];
// open it in a new window / tab (depends on browser setting)
window.open(link);
};
Based on Matyas' answer, I updated the example to work in strict mode. Working Example on Codepen.
Code
HTML
<button id="open-link">Click here to go somewhere else!</button>
JS
"use strict";
// the used links
const links = ["bbc.", "google.", "youtube.", "facebook."];
const openLink = function (links) {
// get a random number between 0 and the number of links
// and round it, so it can be used as array index
const randIdx = parseInt(Math.random() * links.length, 10);
// construct the link to be opened
const link = "https://" + links[randIdx];
// open it in a new window or tab (depends on browser setting)
window.open(link);
};
window.onload = function () {
document.getElementById("open-link").onclick = () => openLink(links);
};
本文标签:
版权声明:本文标题:javascript - How do I open up a random url from a list of url's in a new time each time it is clicked? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745637869a2160560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论