gpt4 book ai didi

javascript - Phonegap - 页面显示但不加载 javascript

转载 作者:行者123 更新时间:2023-11-27 23:54:30 25 4
gpt4 key购买 nike

我有这 2 个 html 文件(经过简化以显示我遇到的问题):

索引.html

<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport"
content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" type="text/css" href="jquery.mobile-1.4.3.css">
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="jquery.mobile-1.4.3.js"></script>
<script type="text/javascript" src="cordova.js"></script>

</head>
<body onload="app.initialize();">

<a href="calendar.html">My Calendar</a>

</body>
</html>

日历.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport"
content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=medium-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" type="text/css" href="jquery.mobile-1.4.3.css">
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="jquery.mobile-1.4.3.js"></script>


</head>
<body onload="app.initialize();">
<a href="index.html">Index</a>

</body>
</html>

index.js:

var app = {

initialize: function() {
this.bindEvents()
},

bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false)
},

onDeviceReady: function() {
app.receivedEvent('deviceready')
},

receivedEvent: function(id){

alert(location.pathname.substring(location.pathname.lastIndexOf("/") + 1))

}

}

现在,当应用程序首次启动时,它会提示消息“index.html”。但在那之后,每次我点击链接在 2 个页面之间跳转时,内容都会显示(指向另一个页面的相应链接),但我没有收到任何警报,这意味着 javascript 没有再次加载。但是,如果我刷新页面,就会出现警报。我希望它无需刷新页面即可加载 javascript。

谢谢,

最佳答案

发生这种情况是因为在您选择下一页时设备已经准备就绪,因此它不会再次触发“设备就绪”事件。

我会将两个页面的结构更改为:

<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport"
content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" type="text/css" href="jquery.mobile-1.4.3.css">
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="jquery.mobile-1.4.3.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="cordova.js"></script>

</head>
<body>

<a href="calendar.html">My Calendar</a>

</body>
</html>

请注意,我已将 index.js 移到 jQuery Mobile 下并删除了“onload”事件。

然后我会利用 jQuery Mobile 的 pagecreate 事件来触发警报

$(document).ready(function(){
app.initalize();
});

var app = {

initialize: function() {
this.bindEvents();
},

bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
$(document).on("pagecontainershow", app.onDeviceReady);
},

onDeviceReady: function() {
app.receivedEvent('deviceready')
},

receivedEvent: function(id){

alert($.mobile.pageContainer.pagecontainer('getActivePage').data('url'));

}

}

关于javascript - Phonegap - 页面显示但不加载 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24852790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com