Dynamically loading CSS/Javascript

3 views
Skip to first unread message

Michael Wyraz

unread,
Mar 11, 2009, 1:57:06 PM3/11/09
to Java2Script
Is it possible to load javascript/css dynamically with J2S?
If not, do you think it should be added to the main lib? I found an
excelent article about this on http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

If it's not possible and not planned, I'll implement a CSS/JS Loader
class and post it here.

Regards,
Michael.

Zhou Renjian

unread,
Mar 11, 2009, 9:19:44 PM3/11/09
to java2...@googlegroups.com
Hi Michael,

There is no such public APIs in net.sf.j2s.*, as we do not want to introduce JavaScript-only APIs to confuse developers. But there are internal APIs about loading CSS:
$WTC$$.registerCSS("org.eclipse.swt.widgets.Button");
Button.css should located in the same path of Button.java with ".swt-widgets-button { width:324px;} " to tell loader that *.css is loaded.

For more, please read

Regards,
Zhou Renjian

Michael Wyraz

unread,
Mar 12, 2009, 4:35:47 AM3/12/09
to Java2Script
I have implemented a simple resource loader that loads css and
javascript on demand. That works fine for CSS but with javascript
there's the problem that the script is loaded after the application is
initialized. Is there a way to load the script before this?

Michael Wyraz

unread,
Mar 12, 2009, 4:49:31 AM3/12/09
to Java2Script
I tried to add my extra js loading code on top of one of the
compiled .js files. In this case it works great. So i'd need a way to
add extra code to the generated js file. Is this somehow possible
(e.g. using annotations)?

Zhou Renjian

unread,
Mar 12, 2009, 4:55:11 AM3/12/09
to java2...@googlegroups.com

Michael Wyraz

unread,
Mar 12, 2009, 6:24:22 AM3/12/09
to Java2Script
I know that. This adds code to the loaded js class. But I need a way
to load code before the class is initialized (at least before the
static constructors are called).

On 12 Mrz., 09:55, Zhou Renjian <zhourenj...@gmail.com> wrote:
> @j2sPrefix in Javadoc
> Tutorial of J2S in Eclipse (7): Advanced Programming on J2S -- @j2s* and
> others<http://j2s.sourceforge.net/articles/tutorial-advanced-programming-on-...>
>
> http://j2s.sourceforge.net/articles/tutorial-advanced-programming-on-...
>
> Regards,
> Zhou Renjian

Michael Wyraz

unread,
Mar 12, 2009, 6:30:47 AM3/12/09
to Java2Script
I think I need to explain a bit more:
I'm implementing wrapper classes for YUI (Yahoo UI widgets) using j2s.
The reason ist that this are simple lightweight and well-documented ui
widgets which are easy to integrate in an existing web page or
application. I started today with this and have very fast progress.
Actually only a few lines (sometimes only one line) of native js code
is reqired for such a wrapper class.
Each YUI component requires one or more JavaScript files plus one or
more CSS files. So my Widgets load them when accessed the first time
using a "static{...}" block. That works great for css but absolutely
not for js. The reason is that the component requires the JS in a very
early stage but it is loaded _after_ the application is initialized
(because it's not loaded by j2slib but by my own the class itself).
So I'm looking for a way to add js code directly to the generated js-
file (above the generated code)

Zhou Renjian

unread,
Mar 12, 2009, 8:25:21 AM3/12/09
to java2...@googlegroups.com
@j2sPrefix add codes before the class is defined. For example:

/**
 * @j2sPrefix
 * alert ("hello");
 */
class A {
...
}

will result in

alert ("hello");
c$ = Clazz.declareType (null, "A");
...

"alert(...)" is called before the class A is defined.

Regards,
Zhou Renjian

Sebastian Gurin

unread,
Mar 12, 2009, 8:41:06 AM3/12/09
to java2...@googlegroups.com
Hi michael. Well, I'm sending attached my YUI java2script port. It is not all YUI, only the dom - events and element part. I know other javascript user have done a jquery port (look at the list history).

By the way, It would be nice to somehow organize all this javascript library ports in one cvs module don't you think?

For resource loading, have you tried an approach like this?:

/**
* @j2sNative
if(window.XMLHttpRequest)
var req = new XMLHttpRequest();
else
var req = new ActiveXObject("Microsoft.XMLHTTP");
req.open("GET",jsFileLocation,false);
req.send(null);
if( window.execScript)
window.execScript(req.responseText);
else
window.eval(req.responseText);
*/
public static void loadScript(String jsFileLocation) {}
--
Sebastian Gurin <sgu...@softpoint.org>
yui.zip

Michael Wyraz

unread,
Mar 12, 2009, 10:08:15 AM3/12/09
to Java2Script
Sebastian,

thanks. I'm implementing exactly the other part (i started with the
widgets).

Zhou,

Thank yout, that helps a bit. The problem ist, that the conde there is
still executed too late. I need to call the code _before_ Clazz.load
is called because It will be evaluated by the vrowser after the loding
code is finished.
>  yui.zip
> 21KAnzeigenHerunterladen
Reply all
Reply to author
Forward
0 new messages