2

I writed and checked my js code in GWT. For checking I added my js code in (projectName).html file and it is worked.

But when I try added external js file I get an error:

WARN] 404 - GET <path to js file>someJsFile.js (127.0.0.1) 1452 bytes
         Request headers

I added this line to (projectName).gwt.xml file:

<script src="src/main/resources/<projectName>/someJsFile.js"></script>
1
  • you can add scripts into index page. Commented Dec 2, 2013 at 11:20

2 Answers 2

8

To use this technique you have to place your someJsFile.js in your public folder so as the gwt compiler copy it to the final folder where it places the html and js stuff.

If you are using maven you have to check if the resources plugin is copying this file to the war and in which path.

By the way, there are other techniques to insert external javascript in your document:

  • Placing the script tag in your .html file.

  • Using ScriptInjector.fromUrl().

  • A better approach is to use a TextResource and ScriptInjector.fromString() so as the compiler reads the javascript file and includes the content in the final compiled file.

  • You can use gwtquery Ajax.getScript to get the script via ajax and inject it in the dom.

  • A new way, recently added to gwtquery, allows you to include your javascript as a JSNI block, so as the compiler can optimize and obfuscate it.

I'd rather the last one because it offers much more advantages.

Sign up to request clarification or add additional context in comments.

3 Comments

public? "src/main/resources/" not public?
"Placing the script tag in your .html file." I put this tag also .html file does not work but the console writes nothing
Normally the convention in GWT is place static files needed for the app in the folder com/yournamespace/public if your module file is for instance com/yournamespace/YourModule.gwt.xml
1

You can define in your_project.gwt.xml which folders to include as public. The paths should be relative to the xml:

resources/
 |-your_project.gwt.xml
 |-subfolder/
   |-stuff/
       |-images/
       |-js/
          |-someJsFile.js

In your xml add:

<public path="subfolder/stuff" />

This should copy images/ and js/ folders into your webapp directory and you can use sth like this for the js file

<script src="js/someJsFile.js"></script>

1 Comment

where i can put <script > ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.