Home > Web Front-end > JS Tutorial > body text

javascript simulates the implementation of JQuery's Ready method and causes problems_javascript skills

WBOY
Release: 2016-05-16 18:40:12
Original
1096 people have browsed it

After the dom is loaded, it is executed. I have never understood it. Based on my lack of understanding of the logic of some methods on the Internet, I read the article "Jquery Source Code Research (ready function)" and then wrote the following code myself (detailed description has been given)

Copy code The code is as follows:

org/1999/xhtml">






< ;body>
test




Because I want to be with jq For comparison, you need to import the jq library during testing. The function itself does not call jq, so please feel free to quote it.

I completed the code through encapsulation, and it can be executed directly with Darren.ready(fn).

After passing the test, a strange problem still occurred: the execution order under FF is jq -> my -> load . In other words, my function can be triggered before the onload event is executed, but it will be later than jq's ready. Still quite satisfied with this.

But the test under IE is: jq -> load -> my. That is to say, although my function can advance the code, it is still triggered after the onload event is executed, which is puzzling.

Comrades, please explain how to implement execution before onload, and how jq is implemented. I completely simulated the structure of jq, but still cannot achieve the goal. Is there a leak in the middle?

You can also refer to the code below

Copy the code The code is as follows:

var ready=function(readyCall) {
if(document.addEventListener)
document.addEventListener("DOMContentLoaded",function() {
document.removeEventListener("DOMContentLoaded",arguments.callee,false);
readyCall();
},false);
else if(document.attachEvent) {//for IE
if(document.documentElement.doScroll && window.self==window.top) {
(function() {
try {
document.documentElement.doScroll("left");
}catch(ex) {
setTimeout(arguments.callee,5);
return;
}
readyCall();
})();
}else {//maybe late but also for iframes
document.attachEvent("onreadystatechange",function() {
if(document.readyState==="complete") {
document.detachEvent("onreadystatechange", arguments.callee);
readyCall();
}
});
}
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template