如何在jquery mobile/phonegap的$(document).ready()/onDeviceReady(
How to load a script on $(document).ready()/onDeviceReady() of jquery-mobile/phonegap
我正在使用PhoneGap和jQuery Mobile开发一个应用程序。
现在,当页面被加载时,我想加载一个script(.js file)
。基本上是CCD_ 2或CCD_。如何做到这一点?
//wait for document.ready to fire
$(function () {
//then load the JavaScript file
$.getScript('script.js');
});
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
第二个代码片段是Google分析代码的一个稍微修改过的版本,用于向DOM异步添加脚本。
更新
您还可以将<script>
标记的defer
属性设置为true
,并且在准备好DOM之后才会执行该属性。请参阅此处的一些文档:https://developer.mozilla.org/en-US/docs/HTML/Element/Script
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- jQuery document.ready not working
- jQuery加载的async和ready函数不工作
- 如何通过documents.ready函数中的javascript自动按键(ctrl+shift+i)
- jQuery document.ready停止代码
- 停止对document.ready函数的重定向/刷新
- $(document).ready和jQuery(function($)不工作;jQuery已定义,但脚本尚未定义;t进
- ready函数在每个页面中触发
- 函数从$(document).ready外部调用在$(document).ready中定义的函数
- 如何使用onDeviceReady设置AngularJS应用程序并初始化Cordova的功能
- 如果没有document.ready(),我怎么能拥有多个javascript函数呢
- ASPX Jquery 1.11 $(Document).ready(function() {} was skipped
- 重载像$(document).ready这样的函数是错误的
- 延迟加载jquery后替换$(document).ready(function)
- $(document).ready未在brunch/jade/主干网项目中调用
- 为什么不'加载$(document.ready(function)后,单击“工作”
- $(document)中的多个javascript函数.ready不工作
- 通过$传递作用域(文档).ready
- 多个“;脚本调用"在SINGLE”内$(文档).ready()”;
- 如何在jquery mobile/phonegap的$(document).ready()/onDeviceReady(