2013年7月28日 星期日

include js files and use them as one js lib

Take a  look at the following html part:

<html>
<head>
<script src="file:\\\android_assets\1st.js"></script>
<script src="file:\\\android_assets\2nd.js"></script>
<script src="file:\\\android_assets\3rd.js"></script>
</head>
</html>

Javascript runtime will sequentially load in JS files, so say if you wanna call a JS function which is part of one of these three JS files but you don't know which one it is, just put the calling operation at the end, or use onload attribute for the last script inclusion, such as:

<html>
<head>
<script src="file:\\\android_assets\1.js"></script>
<script src="file:\\\android_assets\2.js"></script>
<script src="file:\\\android_assets\3.js" onload="function() {target_js_func();}" ></script>  <-method 1
<script>
target_js_func();  <---method 2
</script>
</head>

</html>

This way you won't get undefined error and will get the function working properly.

Edited:
Please refer to a newer post saying that it's not always successful to do it this way because JS files are sometimes not guaranteed loaded completely before calling a JS function inside those external JS files.

Reference links
http://stackoverflow.com/questions/5331921/how-can-i-make-sure-that-some-js-files-are-loaded-before-the-page-continue
http://stackoverflow.com/questions/1795438/load-and-execution-sequence-of-a-web-page

沒有留言:

張貼留言