【问题标题】:switching between multiple URLs in HTML/javascript在 HTML/javascript 中的多个 URL 之间切换
【发布时间】:2015-05-13 10:03:12
【问题描述】:

以下是我想要的示例,但目前有两个缺陷。

  1. 我认为订单不正确,因为我看不到 google.com 以外的任何 url 网站。代码中某些项目的位置必须有一些东西。我试过没有弹出窗口拦截器,但仍然无法显示其他窗口。

  2. 我相信这个程序应该在不同的窗口/标签中打开。我希望我在同一个窗口和选项卡中打开下一个 url 并替换原来的。

    • 谷歌替换为msn; msn 换成了 yahoo

感谢您的帮助。谢谢大家。

代码:

<!DOCTYPE>
<html>
<body>
<script>
var urlList = ['http://www.google.com', 'http://www.msn.com', 'http://www.yahoo.com'];
var wnd;
var curIndex = 0; // a var to hold the current index of the current url

function openWindow(){
    wnd = window.open(urlList[curIndex], '', '');
    setTimeout(function () {
         wnd.close(); //close current window
         curIndex++; //increment the index
         if(curIndex < urlList.length) openWindow(); //open the next window if the array isn't at the end
    }, 2000);
}

openWindow();

</script>
</body>
</html>

【问题讨论】:

  • 仅供参考。对于 html5 文档类型,您的文档类型应为 &lt;!DOCTYPE html&gt;

标签: javascript html url


【解决方案1】:

你可以重复使用你的窗口实例:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var urlList = ['http://www.google.com', 'http://www.msn.com', 'http://www.yahoo.com'];
var wnd;
var curIndex = 0; // a var to hold the current index of the current url

function openWindow(){
    wnd = wnd || window.open();
    wnd.location.href = urlList[curIndex];
    setTimeout(function () {
         curIndex++; //increment the index
         // If all urls have been showed, close our window instance
         if(curIndex < urlList.length) openWindow(); else wnd.close();
    }, 2000);
}

openWindow();
</script>
</html>

【讨论】:

  • 请为您的解决方案提供一个小提琴。谢谢
  • 选择代码 -> [ctrl]+[c] -> [ctrl]+[alt]+[j](打开你的控制台)-> [ctrl]+[v] -> [enter] -> 看看神奇的展开。
  • 我猜他想写在js文件里,而不是在控制台里
  • @Fyre 它将产生与在控制台中运行代码相同的结果,它将在与运行当前页面上嵌入的代码相同的权限下运行。
  • 我想我误解了这个问题。他说它应该在同一个标​​签中打开,而在您的解决方案中它应该在新标签中打开。
【解决方案2】:

如果您在同一页面中打开,您的 javascript 将消失。它行不通。一旦打开 google.com,您的 javascript 就结束了。因此,您应该在新选项卡中打开或考虑使用 iframe。

查看此处了解更多信息: http://www.w3schools.com/tags/tag_iframe.asp

【讨论】:

  • 这不是真的,您仍然可以更改窗口的位置。但是您无法与窗口的内容进行通信。
  • 你怎么能做到这一点?当您打开一个新页面时,javascript 消失了。请赐教
  • var my_window = window.open('https://google.com'); /*....*/ my_window.location.href = 'https://example.com'
  • 当 google.com 打开时。你还有你的javascript吗?您的更新将如何进行。
  • 你仍然可以重置位置对象,这就是我所知道的。
猜你喜欢
  • 1970-01-01
  • 2023-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 2019-02-27
  • 1970-01-01
相关资源
最近更新 更多