本文介绍了是否有可能在iframe上停止/暂停视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是视频
< iframe id =vtwidth =420autoplay =falseheight =345src =http://cache4.asset-cache.net/xd/468140002.mp4?v=1&c=IWSAsset&k=2&d=72990B68305E4FDFEE95B69A879131BCD6D7111452E48A17C03F8342D660D5A451EBC1DEC2A827C9&b=RTdGframeborder =0的allowFullScreen>< / iframe中>
我想停止或暂停,这可能吗?只想贴在iframe上
js / jquery中的任何函数都一样吗?
如果它来自不同的域,则跨站点脚本(XSS)保护将会启动。
我假设您正在播放来自您的域的视频,(嵌入的iframe url域名大部分与页面域相同)。
测试:
去这里:
并在控制台中运行此代码:
var video = undefined;
//在html中定义iframe
ifrm = document.createElement(IFRAME);
ifrm.setAttribute(src,http://cache4.asset-cache.net/xd/468140002.mp4?v=1&c=IWSAsset&k=2&d=72990B68305E4FDFEE95B69A879131BCD6D7111452E48A17C03F8342D660D5A451EBC1DEC2A827C9&b=RTdG );
ifrm.setAttribute(id,vt);
ifrm.setAttribute(width,420);
ifrm.setAttribute(autoplay,false);
ifrm.setAttribute(height,345);
document.body.appendChild(ifrm);
iframe加载事件
ifrm.onload = function(){
var btnPlay = document.createElement(BUTTON);
btnPlay.setAttribute(onclick,play(););
var t1 = document.createTextNode(PLAY ME);
btnPlay.appendChild(t1);
document.body.appendChild(btnPlay);
var btnPause = document.createElement(BUTTON);
btnPause.setAttribute(onclick,pause(););
var t2 = document.createTextNode(PAUSE ME);
btnPause.appendChild(t2);
document.body.appendChild(btnPause);
video = document.getElementById(vt)。contentWindow.document.body.getElementsByTagName('video')[0];
$ b //播放停止方法
函数play()
{
if(video!= undefined)
video.play();
返回false;
函数暂停()
{
if(video!= undefined)
video.pause();
返回false;
}
结论如果您的iframe源与您的域不同,您无法控制视频帧。以上是跨站点脚本不会影响您的情况下如何控制视频。
This is the video
<iframe id="vt" width="420" autoplay="false" height="345" src="http://cache4.asset-cache.net/xd/468140002.mp4?v=1&c=IWSAsset&k=2&d=72990B68305E4FDFEE95B69A879131BCD6D7111452E48A17C03F8342D660D5A451EBC1DEC2A827C9&b=RTdG" frameborder="0" allowfullscreen></iframe>
I want to do stop or pause on it, is that possible? want to stick on iframe only
http://jsfiddle.net/karimkhan/2Lgxk5h3/7/
any function in js/jquery for the same?
解决方案
Yes you can access your video from iframe, but this will work only if the frame source is on the same domain. If it is from a different domain, cross-site-scripting (XSS) protection will kick in.
I suppose you are playing videos from your domain, (embedded iframe url domain most be the same with the page domain).
Testing:
go here:http://cache4.asset-cache.net/
and run this code in console:
var video = undefined;
//define iframe in html
ifrm = document.createElement("IFRAME");
ifrm.setAttribute("src", "http://cache4.asset-cache.net/xd/468140002.mp4?v=1&c=IWSAsset&k=2&d=72990B68305E4FDFEE95B69A879131BCD6D7111452E48A17C03F8342D660D5A451EBC1DEC2A827C9&b=RTdG");
ifrm.setAttribute("id", "vt");
ifrm.setAttribute("width","420");
ifrm.setAttribute("autoplay","false");
ifrm.setAttribute("height","345");
document.body.appendChild(ifrm);
//iframe load event
ifrm.onload = function() {
var btnPlay = document.createElement("BUTTON");
btnPlay.setAttribute("onclick", "play();");
var t1 = document.createTextNode("PLAY ME");
btnPlay.appendChild(t1);
document.body.appendChild(btnPlay);
var btnPause = document.createElement("BUTTON");
btnPause.setAttribute("onclick", "pause();");
var t2 = document.createTextNode("PAUSE ME");
btnPause.appendChild(t2);
document.body.appendChild(btnPause);
video = document.getElementById("vt").contentWindow.document.body.getElementsByTagName('video')[0];
}
//play stop methods
function play()
{
if (video != undefined)
video.play();
return false;
}
function pause()
{
if (video != undefined)
video.pause();
return false;
}
The conclusion if your iframe source is different than your domain you cannot control video from frame. Above is how to control the video when cross site scripting does not afect you.
这篇关于是否有可能在iframe上停止/暂停视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!