简体   繁体   English

$(window).load()在IE和FF上的工作方式不同

[英]$(window).load() works differently on IE and FF

I was working with $(window).load() ...and found one issue. 我正在使用$(window).load() ...并发现了一个问题。 Actually as per this function first window should load and then any implementation under this function should start working. 实际上,根据此功能,第一个窗口应该加载,然后此函数下的任何实现都应该开始工作。 This works fine for IE but not for FF. 这适用于IE但不适用于FF。 in FF its working like $(document).ready() 在FF中它的工作方式就像$(document).ready()

can somebody suggest any alternate approach, or the reason why FF behave like this. 可以有人建议任何替代方法,或FF表现如此的原因。

Your second method is the preferred method, working for both FF and IE: 你的第二种方法是首选方法,适用于FF和IE:

$(document).ready(function(){ /*.code.*/ });

Or the short-hand form: 或者简写形式:

$(function(){ /*.code.*/ });

To add to Jonathans answer, jQuery's own documentation regarding $(document).ready: 要添加到Jonathans的答案,jQuery自己的关于$(document).ready的文档:

"While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts." “虽然JavaScript在呈现页面时提供了执行代码的加载事件,但是在完全接收到所有资源(如图像)之前,不会触发此事件。在大多数情况下,只要DOM层次结构已经运行,脚本就可以运行传递给.ready()的处理程序保证在DOM准备好后执行,因此这通常是附加所有其他事件处理程序并运行其他jQuery代码的最佳位置。当使用依赖于值的脚本时CSS样式属性,在引用脚本之前引用外部样式表或嵌入样式元素很重要。“

source: http://api.jquery.com/ready/ 来源: http//api.jquery.com/ready/

EDIT: 编辑:

Per your comments, it sounds like you are dependent on another javascript executing before you want your script to execute to grab the value. 根据您的意见,听起来您依赖于另一个javascript执行,然后您希望脚本执行以获取值。 I can think of two options here: 我可以在这里想到两个选择:

1) If the other script is in your control, then have it invoke the code you want in your load() functions. 1)如果另一个脚本在你的控件中,那么让它在你的load()函数中调用你想要的代码。

2) If this other code is not in your control, then your best best is to "observe" the field of interest for when it changes, so you know when to invoke your own script. 2)如果这个其他代码不在您的控制范围内,那么您最好的方法是“观察”感兴趣的字段,以便更改它,这样您就知道何时调用自己的脚本。 I don't believe jQuery has this functionality built in (for non form fields), but there are plugins (such as http://plugins.jquery.com/taxonomy/term/1939 ). 我不相信jQuery内置了这个功能(对于非表单字段),但是有插件(例如http://plugins.jquery.com/taxonomy/term/1939 )。

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

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