我有一个问题困扰了我好几天。 简而言之,我有 index.html 页面,当应用程序启动时,它首先为用户显示一个登录页面。在那里我加载了我的文件 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>
所以您可以看到问题出在哪里,当应用程序实际加载 scrips.js 被加载并且应该在 signup.html 上运行的函数 showTab() 在索引页面上传递一次时以及用户尝试去注册时带有 href 和 data-transition="slide-in"的 .html 没有加载 scripts.js 以重新运行函数 showTab() 这是 scripts.js 中的第一行 解决此问题的一种方法是将 data-transition="slide-in"更改为 data-ignore="push",这会导致 signup.html 实际上重新加载,但会丢失转换。 我最终使用 Ratchet 框架和 Cordova 来构建应用程序。 我还尝试在 signup.html 中添加 onload="showTab()"
<div class="content bg-primary" onload="showTab()>
这又不会重新加载 JS。
最佳答案
您不应该尝试使用 Cordova 在页面之间导航。
这是 Best Practices Cordova app development 中的第一项:
First and foremost - your Cordova applications should adopt the SPA (Single Page Application) design. Loosely defined, a SPA is a client-side application that is run from one request of a web page. 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. SPAs are commonly used for more complex client-side applications. GMail is a great example of this. 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.
特别注意这一点:
Even if you choose not to use Cordova, creating a mobile application without using a single page architecture will have serious performance implications. 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.
关于javascript - 重定向到 cordova 应用程序中的另一个页面后无法运行 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629639/