JavaScript弹出式窗口文档永远不会“准备就绪”(Javascript pop

编程入门 行业动态 更新时间:2024-10-24 16:21:12
JavaScript弹出式窗口文档永远不会“准备就绪”(Javascript pop-up window document is never “ready”)

我正在使用Javascript打开一个空白窗口,用最小的填充它并注入一个脚本标记以包含JQuery,但是窗口属性readyState永远不会超过加载,因此JQuery永远不会触发。 这里是jsFiddle的演示 。 我无法弄清楚为什么弹出窗口不更新其readyState。

var popup = window.open("", "popup", "height=500,width=700"); var doc = popup.document; doc.write("<!doctype html><html><head></head><body></body></html>"); var script = doc.createElement("script"); script.type = "text/javascript"; script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"; script.onload = function() { console.log(doc.readyState); popup.$(document).ready(function() { console.log(doc.readyState); }); } doc.getElementsByTagName("head")[0].appendChild(script);

这里是没有JQuery的类似代码 - 与readyState窗口属性相同的问题永远不会超出“加载”。

var popup = window.open("", "popup", "height=500,width=700"); var doc = popup.document; doc.write("<!doctype html><html><head></head><body></body></html>"); console.log(doc.readyState);

I'm using Javascript to open a blank window, populate it with the bare minimum and inject a script tag to include JQuery, but the window property readyState never gets past loading, therefore JQuery never triggers. Here is the demo on jsFiddle. I can't figure out why the popup window doesn't update its readyState.

var popup = window.open("", "popup", "height=500,width=700"); var doc = popup.document; doc.write("<!doctype html><html><head></head><body></body></html>"); var script = doc.createElement("script"); script.type = "text/javascript"; script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"; script.onload = function() { console.log(doc.readyState); popup.$(document).ready(function() { console.log(doc.readyState); }); } doc.getElementsByTagName("head")[0].appendChild(script);

Here is similar code without JQuery--same problem with the readyState window property never going beyond "loading".

var popup = window.open("", "popup", "height=500,width=700"); var doc = popup.document; doc.write("<!doctype html><html><head></head><body></body></html>"); console.log(doc.readyState);

最满意答案

如果您从未设置网址,那么就绪状态不会改变。 它将被“初始化”或“加载” - 取决于您的浏览器。 您可以通过在弹出窗口的控制台窗口中设置document.location来查看此更新,如下面的命令:

console.log(document.readyState); document.location = "http://yourdomain.com"; console.log(document.readyState);

如果您将位置发送到域以外的域,则无法修改窗口对象。 解决这个问题的方法是使用iframe并设置源代码 - 就像这样:

var nw = window.open('', "MyWindowID", "fullscreen=no, toolbar=no, menubar=no,status=no,location=no"); var html = '<iframe id="document-{aid}" style="width:100%;height:100%;" src="http://www.google.com"></iframe>'; nw.document.write(html); nw.focus();

请参阅: https : //stackoverflow.com/a/1443839/1220302

关于这一点的一个注意事项是尝试在$(popup)中包装弹出窗口并挂接ready事件。

这应该适合你:

var popup = window.open("", "popup", "height=500,width=700"); var doc = popup.document; doc.write("<!doctype html><html><head></head><body></body></html>"); var script = doc.createElement("script"); script.type = "text/javascript"; script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"; $(popup).ready(function(){ console.log('loaded'); });

If you never set the URL, then the ready state isn't going to change. It will either be "initialized" or "loading" - depending on your browser. You can see this update by setting the document.location in the console window of your popup window like the command below:

console.log(document.readyState); document.location = "http://yourdomain.com"; console.log(document.readyState);

If you sent the location to a domain other than your domain, you will not have security to modify the window object. A way to get around this is to use an iframe and set the source - like so:

var nw = window.open('', "MyWindowID", "fullscreen=no, toolbar=no, menubar=no,status=no,location=no"); var html = '<iframe id="document-{aid}" style="width:100%;height:100%;" src="http://www.google.com"></iframe>'; nw.document.write(html); nw.focus();

See: https://stackoverflow.com/a/1443839/1220302

A note on this is to try wrapping your popup in $(popup) and hook the ready event.

This should work for you:

var popup = window.open("", "popup", "height=500,width=700"); var doc = popup.document; doc.write("<!doctype html><html><head></head><body></body></html>"); var script = doc.createElement("script"); script.type = "text/javascript"; script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"; $(popup).ready(function(){ console.log('loaded'); });

更多推荐

本文发布于:2023-08-07 07:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出式   永远不会   窗口   文档   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!