Cod*_*LaY 22 javascript jquery jquery-mobile cordova
我正在使用PhoneGap和jQuery Mobile开发应用程序.
现在当页面加载然后我想加载一个script(.js file).基本上onDeviceReady或$(document).ready().怎么做?
Jas*_*per 47
//wait for document.ready to fire
$(function () {
//then load the JavaScript file
$.getScript('script.js');
});
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
http://docs.phonegap.com/en/1.4.1/phonegap_events_events.md.html#deviceready
第二个代码段是Google Analytic代码的略微修改版本,用于异步地向DOM添加脚本.
UPDATE
您还可以将标记的defer属性设置为<script>,true并且在DOM准备好之后才会执行.请参阅此处的一些文档:https://developer.mozilla.org/en-US/docs/HTML/Element/Script
| 归档时间: |
|
| 查看次数: |
40920 次 |
| 最近记录: |