Chrome中的标题重定向后,Onreadystatechange无法正常工作

编程入门 行业动态 更新时间:2024-10-24 12:27:47
本文介绍了Chrome中的标题重定向后,Onreadystatechange无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我制作了一个用于调整图像大小的论坛。它很好,除了在发布或编辑重定向的地方之外,其中 onreadystatechange 不会在Google Chrome中触发。

在查看线程时,用户脚本可以正常工作。

在编辑/发布,EG SomeSite / forums.php?action = viewtopic& topicid = 205362& page = p4976670#4976670 ,userscript不起作用。 <

document.onreadystatechange = function(){ if(document.readyState ==complete){ //调用函数}};

我等待加载所有资源,因为调整大小在具有许多图像的页面上不一致。

该脚本在Firefox中与Greasemonkey完美配合。

任何想法为什么 onreadystatechange 在重定向后不会在Chrome上触发?有没有更好的方法来等待资源被加载?

解决方案

您没有说明您是否使用 @ run-at 参数,但是, < onload 事件,有时它会在(默认情况下)之后触发。如果它位于 onload 事件之后,那么 onreadystatechange 不会触发。

为了适应这种行为,可以将 // @ run-at document-end 添加到元数据块,或将该代码片段替换为:

窗口。 addEventListener(load,YourMainFunction,false); if(document.readyState ==complete){ YourMainFunction(); } 函数YourMainFunction { //在这里做所有的东西... }

I have made a userscript for a forum that resizes images. It works great, except for after posting or editing where it redirects, where onreadystatechange does not fire in Google Chrome.

When viewing a thread, EG SomeSite/forums.php?action=viewtopic&topicid=205362, the userscript works.

After editing/posting, EG SomeSite/forums.php?action=viewtopic&topicid=205362&page=p4976670#4976670, the userscript doesn't work.

The problem lies with this code:

document.onreadystatechange = function () { if (document.readyState == "complete") { //Call function }};

I wait for all resources to be loaded, because the resizing is inconsistent otherwise on pages with many images.

The script works perfectly in Firefox with Greasemonkey.

Any idea why onreadystatechange does not fire on Chrome after a redirect? Is there a better way to wait for resources to have been loaded?

解决方案

You didn't state whether you use the @run-at parameter but, by default, Chrome userscripts are somewhat erratic about when they fire. That means that sometimes a userscript will fire before the onload event and sometimes it will fire after (by default). If it's after the onload event, then onreadystatechange won't trigger.

To adapt to this behavior, either add // @run-at document-end to the metadata block, or replace that code snippet with:

window.addEventListener ("load", YourMainFunction, false); if (document.readyState == "complete") { YourMainFunction (); } function YourMainFunction { // DO ALL YOUR STUFF HERE... }

In the latter case, YourMainFunction() will either wait for the load event (same as readyState == "complete") or fire immediately if the event has already passed. YourMainFunction() will only run once in either case.

更多推荐

Chrome中的标题重定向后,Onreadystatechange无法正常工作

本文发布于:2023-10-12 17:23:05,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   重定向   标题   工作   Chrome

发布评论

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

>www.elefans.com

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