Doc Shell交换示例 [英] Doc Shell Swapping Example

查看:248
本文介绍了Doc Shell交换示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个简单的例子来显示docshell从一个iframe到另一个交换。

我写了这个,可以从scratchpad与环境浏览器运行:

/ p>

  var doc = gBrowser.contentDocument; 
var iframeA = doc.createElement('iframe');
iframeA.setAttribute('id','a');
iframeA.setAttribute('src','http://www.bing.com');
doc.documentElement.appendChild(iframeA);

var iframeB = doc.createElement('iframe');
iframeB.setAttribute('id','b');
iframeB.setAttribute('src','data:text / html,swap to here');
doc.documentElement.appendChild(iframeB);

doc.defaultView.setTimeout(function(){
var srcFrame = iframeA;
var targetFrame = iframeB;
doc.defaultView.alert('will will now ');
srcFrame.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(targetFrame)
doc.defaultView.alert(' 交换完成');
},5000)

然而,当它试图交换时会引发这个错误:

<$ p $类型错误:HTMLIFrameElement.swapFrameLoaders的参数1不实现接口XULElement。 Scratchpad / 1:17
* /

有关如何解决的任何想法?
谢谢

解决方案

感谢来自irc #extdev的@mook和@mossop的帮助。 b
$ b

它没有工作的原因是因为:


  • 很确定它必须是
  • >
  • 看起来像gBrowser.contentDocument可能是一个HTML页面?您必须使用XUL元素

  • 不知道它是否在内容中工作 - 我认为它不是
  • 和要交换的东西必须都是type = content或者都不可以,或者类似的东西(translation:源和目标的 type 属性必须是相同的,所以如果一个是 content-primary 另一个必须是 content-primary


这个工作:

pre code> var doc = document; //gBrowser.contentDocument;
var iframeA = doc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul','browser');
iframeA.setAttribute('id','a');
iframeA.setAttribute('src','http://www.bing.com');
iframeA.setAttribute('style','height:100px;');
doc.documentElement.appendChild(iframeA);

var iframeB = doc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul','browser');
iframeB.setAttribute('id','b');
iframeB.setAttribute('src','data:text / html,swap to here');
iframeB.setAttribute('style','height:100px;');
doc.documentElement.appendChild(iframeB);

doc.defaultView.setTimeout(function(){
var srcFrame = iframeA;
var targetFrame = iframeB;
doc.defaultView.alert('will will now ');
srcFrame.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(targetFrame)
doc.defaultView.alert(' 交换完成');
},5000)




换句话说,即使你使用xul命名空间的iframe,它也不会交换如果你把它放在一个HTML文件中。像这里的例子:

  var xulDoc = document; 
var doc = gBrowser.contentDocument;
var iframeA = xulDoc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul','iframe');
iframeA.setAttribute('id','a');
iframeA.setAttribute('src','http://www.bing.com');
iframeA.setAttribute('type','content');
iframeA.setAttribute('style','height:100px; width:100px;');
doc.documentElement.appendChild(iframeA);

var iframeB = xulDoc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul','iframe');
iframeB.setAttribute('id','b');
iframeB.setAttribute('src','data:text / html,swap to here');
iframeB.setAttribute('type','content');
iframeB.setAttribute('style','height:100px; width:100px;');
doc.documentElement.appendChild(iframeB);

doc.defaultView.setTimeout(function(){
var srcFrame = iframeA;
var targetFrame = iframeB;
doc.defaultView.alert('will will now ');
srcFrame.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(targetFrame)
doc.defaultView.alert(' 交换完成');
},5000)



它引发: NS_ERROR_NOT_IMPLEMENTED:暂存器/ 2:21

这条线是 swapFrameLoaders line






所以建议不要使用带有xul名称空间的iframe,就好像你不会得到DOMContentLoaded和其他一些活动 http:// forums .mozillazine.org / viewtopic.php?f = 19& t = 2809781& hilit = iframe + html + namespace

所以推荐使用元素


I'm trying to write a simple example to show docshell swapping from one iframe to another.

I wrote this, can run from scratchpad with environment browser:

var doc = gBrowser.contentDocument;
var iframeA = doc.createElement('iframe');
iframeA.setAttribute('id', 'a');
iframeA.setAttribute('src', 'http://www.bing.com');
doc.documentElement.appendChild(iframeA);

var iframeB = doc.createElement('iframe');
iframeB.setAttribute('id', 'b');
iframeB.setAttribute('src', 'data:text/html,swap to here');
doc.documentElement.appendChild(iframeB);

doc.defaultView.setTimeout(function() {
  var srcFrame = iframeA;
  var targetFrame = iframeB;
  doc.defaultView.alert('will swap now');
  srcFrame.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(targetFrame)
  doc.defaultView.alert('swap done');
}, 5000)

However this throws this error when it tries to swap:

/*
TypeError: Argument 1 of HTMLIFrameElement.swapFrameLoaders does not implement interface XULElement. Scratchpad/1:17
*/

Any ideas on how to fix? Thanks

解决方案

Thanks to @mook and @mossop from irc #extdev for the help.

The reason it didnt work was because:

  • pretty sure it needs to be a
  • Looks like gBrowser.contentDocument might be a html page? You must be using XUL elements
  • No idea if it even works in content either - I think it doesn't
  • and the things to be swapped must either both be type=content or neither can be, or something like that (translation: the type attribute of both source and target must be the same. so if one is content-primary the other must be content-primary as well)

This works:

var doc = document; //gBrowser.contentDocument;
var iframeA = doc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'browser');
iframeA.setAttribute('id', 'a');
iframeA.setAttribute('src', 'http://www.bing.com');
iframeA.setAttribute('style', 'height:100px;');
doc.documentElement.appendChild(iframeA);

var iframeB = doc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'browser');
iframeB.setAttribute('id', 'b');
iframeB.setAttribute('src', 'data:text/html,swap to here');
iframeB.setAttribute('style', 'height:100px;');
doc.documentElement.appendChild(iframeB);

doc.defaultView.setTimeout(function() {
  var srcFrame = iframeA;
  var targetFrame = iframeB;
  doc.defaultView.alert('will swap now');
  srcFrame.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(targetFrame)
  doc.defaultView.alert('swap done');
}, 5000)


so in other words even if you made iframe with xul namespace it would not swap if you put it in an html document. like this example here:

var xulDoc = document;
var doc = gBrowser.contentDocument;
var iframeA = xulDoc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'iframe');
iframeA.setAttribute('id', 'a');
iframeA.setAttribute('src', 'http://www.bing.com');
iframeA.setAttribute('type', 'content');
iframeA.setAttribute('style', 'height:100px;width:100px;');
doc.documentElement.appendChild(iframeA);

var iframeB = xulDoc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'iframe');
iframeB.setAttribute('id', 'b');
iframeB.setAttribute('src', 'data:text/html,swap to here');
iframeB.setAttribute('type', 'content');
iframeB.setAttribute('style', 'height:100px;width:100px;');
doc.documentElement.appendChild(iframeB);

doc.defaultView.setTimeout(function() {
  var srcFrame = iframeA;
  var targetFrame = iframeB;
  doc.defaultView.alert('will swap now');
  srcFrame.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(targetFrame)
  doc.defaultView.alert('swap done');
}, 5000)

It throws:

NS_ERROR_NOT_IMPLEMENTED:  Scratchpad/2:21

this line is the swapFrameLoaders line


so its recommended to not use iframe with xul namespace as if you do you wont get the DOMContentLoaded and some other events http://forums.mozillazine.org/viewtopic.php?f=19&t=2809781&hilit=iframe+html+namespace

so its recommended to use element

这篇关于Doc Shell交换示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆