我正在使用 PhoneGap 和 jQuery Mobile 开发一个应用程序。
现在,当页面加载时,我想加载一个script(.js file)
。基本上onDeviceReady
or $(document).ready()
。怎么做?
//wait for document.ready to fire
$(function () {
//then load the JavaScript file
$.getScript('script.js');
});
http://api.jquery.com/jquery.getscript http://api.jquery.com/jquery.getscript
//create a callback function
function myCallback () {
//create a script element and set it's type and async attributes
var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true;
//set the source of the script element
script.src = 'script.js';
//add the script element to the DOM
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(script, s);
}
//add event listener for the deviceready function to run our callback function
document.addEventListener("deviceready", myCallback, false);
http://docs.phonegap.com/en/1.4.1/phonegap_events_events.md.html#deviceready http://docs.phonegap.com/en/1.4.1/phonegap_events_events.md.html#deviceready
第二个代码片段是 Google 分析代码的稍微修改版本,用于将脚本异步添加到 DOM。
UPDATE
您还可以设置defer
的属性<script>
tag to true
并且在 DOM 准备好之后才会执行。请参阅此处的一些文档:https://developer.mozilla.org/en-US/docs/HTML/Element/Script https://developer.mozilla.org/en-US/docs/HTML/Element/Script
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)