简体   繁体   English

iframe之后的任何HTML代码都不使用document.write执行

[英]Any HTML code after iframe is not executed using document.write

While I was trying to insert an iframe using documnet.write in IE, I got success. 当我试图在IE中使用documnet.write插入iframe时,我获得了成功。 But, any html code after that is not executed. 但是,之后的任何html代码都不会被执行。

document.write("<div>Hello</div><iframe ..../><div>Bye Bye</div>");

Here "Bye Bye" string is not executed. 这里“Bye Bye”字符串未执行。

For an instant check you can type in your browser url 如需即时检查,您可以输入您的浏览器网址

javascript:document.write("<div>Hello</div><iframe ..../><div>Bye Bye</div>");

After doing trial and error, I found that if I close the iframe tag in the following way, it works. 经过反复试验后,我发现如果我按照以下方式关闭iframe标签,它就能正常工作。

<iframe ...></iframe> instead of <iframe  ...  />

Now, the problem is "I do not have any opportunity to change the <iframe ../> to <iframe .. ></iframe> ". 现在,问题是“我没有机会将<iframe ../>更改为<iframe .. ></iframe> ”。 Looking for your kind advice. 寻找你的好意见。

If you are allowed to change the string before it is sent to document.write then you could modify the markup yourself like so: 如果允许在将字符串发送到document.write之前更改字符串,那么您可以自己修改标记:

markup = "<div>Hello</div><iframe ..../><div>Bye Bye</div>"
markup = markup.replace(/<iframe([^>]*?)\/>/g, '<iframe$1></iframe>')
document.write(markup);

If you don't have any opportunity to change both the string (ie you can not read it beforehand) and the document.write part of the code, I assume you at least have permission to edit/insert some code in the document (or else you should not have this question as you can literally do nothing at all). 如果您没有任何机会更改字符串(即您无法事先阅读)和document.write部分代码,我假设您至少有权在文档中编辑/插入一些代码(或否则你不应该有这个问题因为你根本就什么都不做。

That way, try tempering the document.write() method by inserting some code before things happen: 这样,尝试通过在事情发生之前插入一些代码来调整document.write()方法:

document.write=function(str){
    str=str.replace(/<iframe([^>]*?)\/>/ig,'<iframe$1></iframe>');
    return document.writeln(str);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM