JavaScript 随机 link 生成并在新选项卡中打开它们
JavaScript random link generation and opening them in a new tab
在正常情况下,我可以 link Amazon Mechanical Turk 用户只需将我的调查 URL 复制并粘贴到表单中即可。当用户单击此 URL 时,他们将被发送到 新 选项卡中的此页面。
我想用 hyperlink 替换此 fixed URL,随机将用户重定向到四分之一的网页。在帮助下,我能够编写生成四个不同站点的数组的代码,并能够随机 select 并将其中之一呈现给用户。我对这段代码的问题是,在单击这个新的 (hyper)link 后,它们会在 纯文本 中向其中一个站点显示 URL,并且在相同 window。
我想要做的是将用户直接发送到 new[= 数组中随机 selected 的站点之一33=] 选项卡,当他们单击此 URL 时。简而言之:让它的行为与直接 hyperlink 到 URL 的行为相同。这样做的原因是用户必须保持将他们重定向到不同站点的页面打开,因为他们需要在完成调查后填写代码。
我不明白为什么这还没有发生,以及我应该如何调整代码来实现这一点。如果能帮助解决这个问题,我将不胜感激。
<a href="JavaScript:openSite()">Click to go to a random site</a>
<script>
var links = [
"google.com",
"youtube.com",
"whosebug.com",
"apple.com"]
var openSite = 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];
return link;
};
</script>
尝试如下
<a href="javascript:openSite()">Click to go to a random site</a>
<script>
var links = [
"google.com",
"youtube.com",
"whosebug.com",
"apple.com"]
var openSite = 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];
var win = window.open(link, '_blank');
win.focus();
};
</script>
在正常情况下,我可以 link Amazon Mechanical Turk 用户只需将我的调查 URL 复制并粘贴到表单中即可。当用户单击此 URL 时,他们将被发送到 新 选项卡中的此页面。
我想用 hyperlink 替换此 fixed URL,随机将用户重定向到四分之一的网页。在帮助下,我能够编写生成四个不同站点的数组的代码,并能够随机 select 并将其中之一呈现给用户。我对这段代码的问题是,在单击这个新的 (hyper)link 后,它们会在 纯文本 中向其中一个站点显示 URL,并且在相同 window。
我想要做的是将用户直接发送到 new[= 数组中随机 selected 的站点之一33=] 选项卡,当他们单击此 URL 时。简而言之:让它的行为与直接 hyperlink 到 URL 的行为相同。这样做的原因是用户必须保持将他们重定向到不同站点的页面打开,因为他们需要在完成调查后填写代码。
我不明白为什么这还没有发生,以及我应该如何调整代码来实现这一点。如果能帮助解决这个问题,我将不胜感激。
<a href="JavaScript:openSite()">Click to go to a random site</a>
<script>
var links = [
"google.com",
"youtube.com",
"whosebug.com",
"apple.com"]
var openSite = 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];
return link;
};
</script>
尝试如下
<a href="javascript:openSite()">Click to go to a random site</a>
<script>
var links = [
"google.com",
"youtube.com",
"whosebug.com",
"apple.com"]
var openSite = 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];
var win = window.open(link, '_blank');
win.focus();
};
</script>