开发者

How to run Javascript code before document is completely loaded (using jQuery) [closed]

开发者 https://www.devze.com 2023-01-03 04:13 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question 开发者_开发问答so that it can be reopened, visit the help center. Closed 12 years ago.

I am sharing a tip with you all.Please add on to this discussion.

JQuery helps faster page load than javascript. JQuery functions are fired when the related elements are loaded, instead of complete pageload. This is a common practice to call a javascript function when page is loaded like

window.onload = function(){ alert("Mindfire") }

or

<body onload="javascript:document.getElementById('user_id').focus();">

Inside of which is the code that we want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using window.onload in the first place is due to the fact that the HTML 'document' isn't finished loading yet, when you first try to run your code. To circumvent both problems, jQuery has a simple statement that checks the document and waits until it's ready to be manipulated, known as the ready event

$(document).ready(function()

{
   // Your code here
 });


if you want to run a script before a document is ready then simply put inline script like

<div> .........................</div>
<div> another division </div>
<script>alert('hi');</script>
<div id="this">...................</div>
.
.
.
.
.


Use document ready:

http://www.learningjquery.com/2006/09/introducing-document-ready

$(document).ready(function() {
  // put all your jQuery goodness in here.
});
0

精彩评论

暂无评论...
验证码 换一张
取 消