简体   繁体   English

重定向到Cordova应用程序中的另一个页面后无法运行javascript

[英]Can't run javascript after redirecting to another page in cordova app

I have a problem that's been troubling me for days. 我有一个困扰我几天的问题。 In short i have index.html page that displays first a login page for the user when the app starts. 简而言之,我有index.html页面,该页面在应用启动时首先为用户显示一个登录页面。 In there i load my file scripts.js which cotains functions for all of the pages (including signup.html), but the problem is there is a function showTab() that has to run when the user wants to create account and gets redirected to the signup.html page I do that like this: 在其中加载我的文件scripts.js,其中包含所有页面的功能(包括signup.html),但是问题是有一个showTab()函数必须在用户想要创建帐户并重定向到时运行。在signup.html页面上,我这样做是这样的:

<a class="text-info font-weight-bold h5 text-underline"  href="signup.html" data-transition="slide-in"><U>Create
one now!</U></a> 

So you can see where the problem is when the app actually loads the scrips.js is loaded and the function showTab() which should run on signup.html passes once on the index page and when the user tried to go to signup.html with the href and data-transition="slide-in" the scripts.js is not loaded all over to re-run the function showTab() which is first line in scripts.js One way to solve this is to change data-transition="slide-in" to data-ignore="push" which causes the signup.html to reload actually but that loses the transition. 因此,您可以看到问题出在哪里,当应用程序实际加载scrips.js时,加载应在signup.html上运行的showTab()函数在索引页面上传递一次,而用户尝试通过以下方式转到signup.html: href和data-transition =“ slide-in”不会完全加载scripts.js来重新运行showTab()函数,此函数是scripts.js的第一行。解决此问题的一种方法是更改​​data-transition = “滑入”到data-ignore =“ push”,这实际上导致signup.html重新加载,但丢失了过渡。 I use Ratchet framework and Cordova to build the app ultimately. 我最终使用Ratchet框架和Cordova来构建应用程序。 I tried also adding onload="showTab()" in signup.html 我也尝试在signup.html中添加onload =“ showTab()”

<div class="content bg-primary" onload="showTab()>

Which again doesn't reload the JS. 再次不会重新加载JS。

You should not try to navigate between pages with Cordova. 应该试图用科尔多瓦页面之间进行导航。

This is the first item from the Best Practices Cordova app development : 这是最佳实践Cordova应用程序开发的第一项:

First and foremost - your Cordova applications should adopt the SPA (Single Page Application) design. 首先-您的Cordova应用程序应采用SPA(单页应用程序)设计。 Loosely defined, a SPA is a client-side application that is run from one request of a web page. 松散定义的SPA是从网页的一个请求运行的客户端应用程序。 The user loads an initial set of resources (HTML, CSS, and JavaScript) and further updates (showing a new view, loading data) is done via AJAX. 用户加载一组初始资源(HTML,CSS和JavaScript),并通过AJAX完成进一步的更新(显示新视图,加载数据)。 SPAs are commonly used for more complex client-side applications. SPA通常用于更复杂的客户端应用程序。 GMail is a great example of this. GMail就是一个很好的例子。 After you load GMail, mail views, editing, and organization are all done by updating the DOM instead of actually leaving the current page to load a completely new one. 加载GMail之后,邮件视图,编辑和组织工作都是通过更新DOM来完成的,而不是实际离开当前页面来加载一个全新的页面。

Pay special attention to this: 请特别注意:

Even if you choose not to use Cordova, creating a mobile application without using a single page architecture will have serious performance implications. 即使您选择不使用Cordova,在不使用单页体系结构的情况下创建移动应用程序也会对性能产生严重影响。 This is because navigating between pages will require scripts, assets, etc., to be reloaded. 这是因为在页面之间导航将需要重新加载脚本,资产等。 Even if these assets are cached, there will still be performance issues. 即使这些资产被缓存,仍然会存在性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM