, jQuery ready, :
- - "" : , DOM
ready, , ,- jQuery
ready , , , DOM
, HTML-, , :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page title</title>
<link rel="stylesheet" href="/path/to/file.css">
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="jquery.plugins.js"></script>
<script src="yourcode.js"></script>
</body>
</html>
jquery.plugins.js - :
$.fn.jQueryExample = function(){
};
$.fn.somePlugin = function(){
};
function nativeExample(a, b)
{
return a + b;
}
yourcode.js :
$(function(){
$('select').jQueryExample();
nativeExample();
});
, what would happen as opposed to having it defined outside but called inside document ready . :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page title</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
// placing <script> tag in the <head> tag is considered as a bad practice
function getFoo() {
return "foo";
}
function getAnchor() {
return document.getElementsByTagName('a');
}
</script>
<script>
console.log( "one", getFoo() );
console.log( "one", getAnchor() );
</script>
<script>
$(document).ready(function(){
console.log( "two", getFoo() );
console.log( "two", getAnchor() );
});
</script>
</head>
<body>
<a href="www.example.com">bar</a>
<script>
console.log( "three", getFoo() );
console.log("three", getAnchor() );
</script>
<script>
$(document).ready(function(){
console.log( "four", getFoo() );
console.log( "four", getAnchor() );
});
</script>
</body>
</html>
getFoo DOM. , 4 "foo", , ( "" ).
getAnchor DOM . script "ONE" ready undefined. script "THREE" ready , . , . , , , undefined. , , . , getAnchor , DOM . , - :
one foo
one []
three foo
three [<a href="www.example.com">bar</a>]
two foo
two [<a href="www.example.com">bar</a>]
four foo
four [<a href="www.example.com">bar</a>]
: , , , ; : , , , . ready .