最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to use JavaScript with gwt Uibinder - Stack Overflow

programmeradmin0浏览0评论

I want to execute JavaScript in my application which is written with GWT. This is the code that works in a regular HTML:

<html>
  <head>
  </head>

  <body>
    <ul>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
    <script src="jquery.js"></script>
    <script src="jquery.roundabout.js"></script>
    <script>
      $(document).ready(function() {
          $('ul').roundabout();
      });
    </script>
  </body>
</html>

(Assume jquery.js and jquery.roundabout.js are in the same folder.)

I have a somePresenter (with someView.java and someView.ui.xml) and I want the above code to work in the someView.ui.xml.

I copy pasted the above code to the XML (except for <html> tag I have <g:HTMLPanel> tag) but the JavaScript doesn't seem to be executed (all I see is the list as it should be before the script).

How can I make it work?

Related question: can I some how use GQuery for this (something like: GQuery.$().getScript(jquery.roundabout.js) to load external js script)?

Thanks in advance

I want to execute JavaScript in my application which is written with GWT. This is the code that works in a regular HTML:

<html>
  <head>
  </head>

  <body>
    <ul>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
    <script src="jquery.js"></script>
    <script src="jquery.roundabout.js"></script>
    <script>
      $(document).ready(function() {
          $('ul').roundabout();
      });
    </script>
  </body>
</html>

(Assume jquery.js and jquery.roundabout.js are in the same folder.)

I have a somePresenter (with someView.java and someView.ui.xml) and I want the above code to work in the someView.ui.xml.

I copy pasted the above code to the XML (except for <html> tag I have <g:HTMLPanel> tag) but the JavaScript doesn't seem to be executed (all I see is the list as it should be before the script).

How can I make it work?

Related question: can I some how use GQuery for this (something like: GQuery.$().getScript(jquery.roundabout.js) to load external js script)?

Thanks in advance

Share Improve this question edited Aug 13, 2012 at 19:01 stakx - no longer contributing 84.8k20 gold badges174 silver badges277 bronze badges asked Apr 16, 2012 at 16:31 MichaelMichael 9884 gold badges11 silver badges18 bronze badges 1
  • Why wouldn't you just put jQuery and plugin scripts in your host page? With proper caching configured it will not impact your application performance. – Strelok Commented Apr 17, 2012 at 4:59
Add a ment  | 

2 Answers 2

Reset to default 8

You can't put a <script> in UiBinder and expect to see it loaded and executed for the same reasons you cannot put a <script> in a innerHTML in JS with the same expactations (simply because HTMLPanel and UiBinder will use innerHTML under-the-hood).

If you need to load scripts dynamically, have a look at the ScriptInjector:

ScriptInjector.fromUrl(GWT.getModuleBaseURL() + 'jquery.js').inject();

I suppose GQuery uses ScriptInjector or works similarly.

I eventually managed to do this by putting the

<script src="jquery.js"></script>
<script src="jquery.roundabout.js"></script>

in the project.html and then executing the relevant JavaScript with JSNI call when the relevant page loaded (my presenter).

发布评论

评论列表(0)

  1. 暂无评论