admin管理员组

文章数量:1431401

Why use ScriptInjector class.

What is the difference between using ScriptInjector to inject a js file and adding the link with a <script> tag directly to the index.html file. Is there any advantage of using the ScriptInjector over adding the script tag in the index.html file.

One thing that es to my mind is that the initial load may be a lot more if you add a ton of scripts in the index.html file using <script> which are not going to be used until later. ScriptInjector will cut down on the initial load by loading the script asynchronously only when required.

Why use ScriptInjector class.

What is the difference between using ScriptInjector to inject a js file and adding the link with a <script> tag directly to the index.html file. Is there any advantage of using the ScriptInjector over adding the script tag in the index.html file.

One thing that es to my mind is that the initial load may be a lot more if you add a ton of scripts in the index.html file using <script> which are not going to be used until later. ScriptInjector will cut down on the initial load by loading the script asynchronously only when required.

Share Improve this question asked Apr 20, 2015 at 17:00 Sai Phaninder Reddy JSai Phaninder Reddy J 4445 silver badges8 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

The main advantage of using ScriptInjector is that you can leverage the Java language to inject JS files (or JS code) in your page. In the context of a GWT application, this is cleaner than using JSNI.

You're right, adding the <script> tags up front in the HTML file might lead to performance issues.

Advantages of Using ScriptInjector are

  • ScriptInjector Dynamically create script tag and attach it to DOM meaning based on the condition you can inject script(js).
  • If we include lot of Script tag on host page, which will take lot of time to load the script tag(also it will block untill its get executed).

You already described one of the key advantages.

Another advantage is that some scripts may never be used. For example, you may have a script that integrates Google Wallet payments or Facebook login, but not every user needs these features every time. So there is no reason to load these scripts until actually needed.

本文标签: javascriptGWT ScriptInjector VS adding script tag to indexhtml pageStack Overflow