javascript - Document.ready in external files? -
i referencing javascript follows on html page:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script> <script type="text/javascript" src="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js"></script> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&region=gb"></script> <script type="text/javascript" src="js/shared.js"></script> <script type="text/javascript"> $('document').ready(function() { // in-page code: call functions in shared.js }); </script>
the functions defined in shared.js not wrapped inside $('document').ready
. so:
is safe assume functions defined in
shared.js
available "in-page code"?if pull out in-page code separate file called
local.js
(keeping wrapped in$('document').ready
), still safe assume functions defined inshared.js
available?finally, fact i'm not wrapping shared.js inside
$('document').ready
problem? i'm finding if wrap it, functions no longer available in-page code.
the reason question 3 i'm hitting problem: uncaught typeerror: property ... not function - after page has loaded
and wondering if how i've organised code.
update: answers. it's clear using $('document').ready
in shared.js remove functions global scope. however, want clarify original question in point 3.
can assume if following:
- inside in-page code, loaded inside
$('document').ready
, call function shared.js - have function in shared.js refer jquery, google maps, or elements on page
there no problems?
in other words, safe assume page have loaded time functions inside shared.js
called, if i'm not wrapping in file inside $('document').ready
?
is safe assume functions defined in shared.js available "in-page code"?
yes, long functions injected global scope
if pull out in-page code separate file called local.js (keeping wrapped in $('document').ready), still safe assume functions defined in shared.js available?
yes, long local.js
included after shared.js
and shared.js injects functions global scope.
finally, fact i'm not wrapping shared.js inside $('document').ready problem? i'm finding if wrap it, functions no longer available in-page code.
wrapping functions in document.ready
takes them outside of global scope.
var foo = 4; // global $(function() { var bar = 5; // local }); foo = bar; // error
you need inject variables in global scope, easy doing
$(function() { /* code */ window["someglobalvariable"] = somefunctioniwantglobal; });
Comments
Post a Comment