本文介绍了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 { //在这里做所有的东西... } 在后一种情况下, YourMainFunction()将等待加载事件(与 readyState ==complete相同),或者如果事件已经通过,则立即触发。无论哪种情况, 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无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 02:43