使用 iframe 嵌入 Youtube 视频会导致“不安全的 JavaScript 尝试......”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13625239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embedding Youtube video using iframe gives "Unsafe JavaScript attempt ... "
提问by AbdelHady
I'm trying to embed a YouTube video with this code:
我正在尝试使用以下代码嵌入 YouTube 视频:
<iframe width="425" height="319" frameborder="0" wmode="Opaque"allowfullscreen=""
src="http://www.youtube.com/embed/8vJwFvFi4ZY?wmode=transparent">
</iframe>
and although it is working fine, but it gives this error in the console:
虽然它工作正常,但它在控制台中给出了这个错误:
Chrome version 22.0.1229.94:
Unsafe JavaScript attempt to access frame with URL http://example.com/
from frame with URL http://www.youtube.com/embed/8vJwFvFi4ZY?wmode=transparent.
Domains, protocols and ports must match.Firefox version 17.0:
Error: Permission denied to access property 'toString'
Chrome 版本 22.0.1229.94:
不安全的 JavaScript 尝试 从 URL http://www.youtube.com/embed/8vJwFvFi4ZY?wmode=transparent 的框架访问 URL http://example.com/
的框架。 域、协议和端口必须匹配。火狐 17.0 版:
错误:访问属性“toString”的权限被拒绝
I searched around but I found that it is probably a YouTube issue and they should solve it,
我四处搜索,但我发现这可能是 YouTube 的问题,他们应该解决它,
The question is: how can I get rid of this error? (by any means, even by suppressing it.)
问题是:我怎样才能摆脱这个错误?(无论如何,甚至通过压制它。)
采纳答案by David Hellsing
You can't stop it, at least not in any way I know (and I have tried a lot). There is a script in the iframe destination that tries to access your document, probably looking for global functions it can call to enable the API.
你无法阻止它,至少在我知道的任何方面都无法阻止(而且我已经尝试了很多)。iframe 目标中有一个脚本试图访问您的文档,可能会寻找它可以调用以启用 API 的全局函数。
Another thing is that the error persists even when using their own iframe API: http://jsbin.com/izabed/1/edit
另一件事是,即使使用他们自己的iframe API,错误仍然存在:http: //jsbin.com/izabed/1/edit
There is no harm in this, your video will work fine. But it looks kind of bold if you run it in a console. They should probably include this as a parameter, and at first I thought that this was the idea of the origin
parameter, but it doesn't make any difference.
这没有坏处,您的视频会正常工作。但是如果你在控制台中运行它,它看起来有点大胆。他们可能应该将此作为参数包含在内,起初我认为这是origin
参数的想法,但它没有任何区别。
It's also worth noting that their own demodisplays the same error (and others). Also, if you use the embed
tag instead of iframe, it wont display any errors.
还值得注意的是,他们自己的演示显示了相同的错误(和其他错误)。此外,如果您使用embed
标签而不是 iframe,它不会显示任何错误。
So you could do something like this to prevent the error in most desktop browsers:
所以你可以做这样的事情来防止大多数桌面浏览器中的错误:
if(haveflash) {
// use <embed>
} else {
// use iframe
}
回答by Kārlis Millers
Just add ?html5=1
to prevent this error. (Swich to HTML5 player)
只需添加?html5=1
以防止此错误。(切换到 HTML5 播放器)
回答by user1803112
Moving the discussion from comments to this answer. In-short, the problem is that cross domain JS object access is not allowed, which in your case, a script at youtube.com is trying to do to the parent page.
将讨论从评论转移到这个答案。简而言之,问题是不允许跨域 JS 对象访问,在您的情况下,youtube.com 上的脚本试图对父页面执行此操作。
If you only want to show the youtube video, you can use <embed>
tag instead.
如果您只想显示 youtube 视频,则可以改用<embed>
标签。
回答by Bic Mitchun
Why use Javascript when you can only use CSS? It works every.single.time. You can even incorporate any video in a responsive slider.
当您只能使用 CSS 时,为什么要使用 Javascript?它每一次都有效。您甚至可以在响应式滑块中加入任何视频。
OR just visit: http://embedresponsively.com/
或者只是访问:http: //embedresponsively.com/
<style>
.embed-container
{
position: relative;
padding-bottom: 56.25%;
height: 0;
overflow: hidden;
max-width: 100%;
}
.embed-container iframe, .embed-container object, .embed-container embed
{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
<div class='embed-container'>
<iframe src='https://www.youtube.com/watch?v=tntOCGkgt98' frameborder='0' allowfullscreen>
</iframe>
</div>
回答by user1803112
What browser is it ? When I try to append an iframe using JavaScript in Firefox in this page, the error/warning does not appear.
它是什么浏览器?当我尝试在此页面的 Firefox 中使用 JavaScript 附加 iframe 时,未出现错误/警告。