【问题标题】:Select Random URL and display in Embed window选择随机 URL 并显示在嵌入窗口中
【发布时间】:2019-09-26 14:52:55
【问题描述】:

我想要实现的是选择一个随机 URL,然后该 URL 的页面显示在嵌入中,然后您可以单击并转到该页面。但是,我遇到了麻烦,希望能提供任何帮助。这是我到目前为止得到的:

<script type="text/javascript">

var urls = new Array();
urls[0] = "https://www.google.com";
urls[1] = "https://www.yahoo.com";
urls[2] = "https://www.bing.com";

var random = Math.floor(Math.random()*urls.length);

window.location = urls[random];

<embed src="urls" style="width:500px; height: 300px;">
</script>

【问题讨论】:

  • &lt;embed&gt; 标签不能放在&lt;script&gt; 标签内。然后你应该设置 embed 标签的 src 属性,而不是 window.location;这就是页面本身。

标签: javascript html


【解决方案1】:

您的代码中有很多错误,例如您使用window.location = ... 设置当前窗口的位置而不是设置&lt;iframe&gt; 的URL,或者您的JS 中有HTML 代码等.

在任何情况下,您都应该选择之前的 URL,然后然后&lt;iframe&gt; 添加到 DOM 并设置其 url。这是一个例子:

const urls = [
  'https://example.com',
  'https://example.org',
];

const iframe = document.createElement('iframe');
iframe.src = urls[Math.floor(Math.random()*urls.length)];
document.body.appendChild(iframe);

【讨论】:

  • 好的,非常感谢...您知道如何通过重定向到该页面来使窗口可点击吗?
  • 已经有一个关于 SO 细节的问题how to use an iframe as a link
  • &lt;embed&gt;标签包装在一个&lt;a&gt;标签中,其url与href属性相同。
猜你喜欢
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多