简体   繁体   English

防止HTML Script元素多次运行

[英]Prevent HTML Script element from running multiple times

I am using Ionic 2 to develop my app. 我正在使用Ionic 2开发我的应用程序。 Within index.html there are several scripts that will run at the start of the application. index.html ,有几个脚本将在应用程序的开头运行。

I want to re-run app.bundle.js halfway when a user is using the app. 我希望在用户使用该应用程序时重新运行app.bundle.js

I have done something like this: 我做过这样的事情:

var script = document.createElement("script");
script.type = 'text/javascript';
script.src = 'build/js/app.bundle.js';
document.body.appendChild(script);

In android, this works fine. 在android中,这很好用。

For iOS, after running this code, the app refreshes but my console.log shows multiple instances of each line. 对于iOS,运行此代码后,应用程序刷新,但我的console.log显示每行的多个实例。 I am assuming the code starts a new cycle of the script but the original one is still running. 我假设代码开始一个新的脚本循环但原始的仍在运行。

Anybody has any idea how to prevent this in iOS? 任何人都知道如何在iOS中防止这种情况?

To check whether you have multiple scripts running, you can do this: 要检查是否有多个脚本在运行,可以执行以下操作:

var viewScripts = document.getElementsByTagName("script");

Let's say you wish to the first element, you can do this: 假设您希望第一个元素,您可以这样做:

viewScripts[0].parentElement.removeChild(b[0]);

You can check if this prevents the multiple instances. 您可以检查这是否会阻止多个实例。 Else it may be caused by another issue and not the scripts. 否则,它可能是由另一个问题而不是脚本引起的。

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

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