JavaScript不能在除第一页之外的phonegap上工作? [英] JavaScript does not work on phonegap other than the first page?

查看:189
本文介绍了JavaScript不能在除第一页之外的phonegap上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个phonegap应用程序,第一页通过href导航到第二页。
在第二页,我有一个JavaScript函数,我想执行。

I created a phonegap application, the first page navigates to the second page by "href". in the second page, I have a JavaScript function that I want to execute.

问题是页面不知道JavaScript,只是我将函数复制到
第一页 - 它工作。

The problem is that the page doesn't know the JavaScript, just if I copy the function to the first page - it works.

我在页面中包含JavaScript。

I include the JavaScript in the page.

有什么问题?

推荐答案

对于应用程序中的每个页面,在该页面上执行。例如在page1.html你有一个被称为getData()的引用函数,你将能够在page1.html上调用它。如果你遵循一个href到page2.html函数getData()现在超出范围。值得一提的是,这正是在网络浏览器中的工作方式。

For each page in the application you will have to include the JavaScript code you wish to execute on that page. For instance on page1.html you have a referenced function called getData() you will be able to call it on page1.html. If you follow a href to page2.html the function getData() is now out of scope. It is worth mentioning this is exactly how things work in web browsers.

这样做的方法是将getData()移动到外部JavaScript文件,如main.js。然后通过page1.html和page2.html中的脚本标记引用main.js。现在您可以从任一页面调用getData()。

The way around this is to move getData() to an external JavaScript file like main.js. Then you reference main.js via a script tag in both page1.html and page2.html. Now you'll be able to call getData() from either page.

这篇关于JavaScript不能在除第一页之外的phonegap上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆