简体   繁体   English

Wicket 1.5正文标记中的Javascript文件

[英]Wicket 1.5 Javascript File in Body Tag

I need to put a script-tag inside the body, because the javascript doesn't work in the head tag. 我需要在body中放置一个script-tag,因为javascript在head标签中不起作用。

As of Wicket 1.5 the following code is not longer supported: 从Wicket 1.5开始,不再支持以下代码:

add(new JavaScriptReference("wz_tooltip", new JavaScriptResourceReference(BaseTemplate.class, "wz_tooltip.js")));

This is because the class JavaScriptReference no longer exists. 这是因为JavaScriptReference类不再存在。

I can't obviously use the new renderHead method. 我显然不能使用新的renderHead方法。

My workaround so far: 到目前为止我的解决方法:

WebMarkupContainer script = new WebMarkupContainer("script");
script.add(new AttributeAppender("type", Model.of("text/javascript")));
script.add(new AttributeAppender("src", Model.of("wz_tooltip.js")));
add(script);

Really ugly apart from that it doesn't work; 真的很丑,除了它不起作用; can't find the javascript files. 找不到javascript文件。

Is there any other workaround or a "right" way to do it? 有没有其他解决方法或“正确”的方法来做到这一点?

Have you tried using a urlFor with a resource reference? 您是否尝试过使用带有资源引用的urlFor? Something like.. 就像是..

script.add(new AttributeAppender("src", urlFor(new JavaScriptResourceReference(BaseTemplate.class, "wz_tooltip.js"), null).toString())); 

I wouldn't expect your current method to work out of the box because of Wicket's complex resource management. 由于Wicket的复杂资源管理,我不希望您当前的方法能够开箱即用。 Also, while there may be other "right" ways to do things, I've often found that 3rd-Party javascript can be finicky about placement and I still need to fall back on this method for some scripts. 此外,虽然可能有其他“正确”的方法来做事,但我经常发现第三方javascript对于放置很挑剔,我仍然需要依靠这种方法来处理某些脚本。

For example, one script I use must be the last element on the page. 例如,我使用的一个脚本必须是页面上的最后一个元素。

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

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