通过wicket动态编写脚本src

发布于 2024-12-18 14:45:04 字数 332 浏览 0 评论 0原文

我希望我的页面将 javascript 动态加载到我的 body

<script type= "text/javascript" src="this path should be decided from wicket dynamically"/>

我使用的是 wicket 版本 1.4,因此我的版本中不存在 JavaScriptResourceReference (据我检查,不是')

我该如何解决这个问题? 提前致谢 :)。

I want my page to load javascript dynamically to my body:

<script type= "text/javascript" src="this path should be decided from wicket dynamically"/>

I am using wicket version 1.4 therefore JavaScriptResourceReference does not exist in my version (for my inspection it wasn't ' )

how can I solve this ?
thanks in advance :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神妖 2024-12-25 14:45:04

我将我的评论指定为答案。

您可以使用此代码片段:

WebMarkupContainer scriptContainer = new WebMarkupContainer("scriptContainer ");
scriptContainer .add(new AttributeAppender("type", Model.of("text/javascript")));
scriptContainer .add(
    new AttributeAppender("src", urlFor(
        new JavaScriptResourceReference(
            YourClass.class, "JavaScriptFile.js"), null).toString()));
add(scriptContainer );

和相应的 html:

<script wicket:id="scriptContainer "></script>

只需更改字符串 JavaScriptFile.js 即可加载任何其他 Javascript 文件。

I specify my comment into an answer.

You can use this code snippet:

WebMarkupContainer scriptContainer = new WebMarkupContainer("scriptContainer ");
scriptContainer .add(new AttributeAppender("type", Model.of("text/javascript")));
scriptContainer .add(
    new AttributeAppender("src", urlFor(
        new JavaScriptResourceReference(
            YourClass.class, "JavaScriptFile.js"), null).toString()));
add(scriptContainer );

and the corresponding html:

<script wicket:id="scriptContainer "></script>

Just change the string JavaScriptFile.js to load any other Javascript file.

套路撩心 2024-12-25 14:45:04

< code>JavascriptPackageResource.getHeaderContributor() 正是您所需要的。

您不需要在标记中添加任何内容,只需添加它返回到您的页面的 HeaderContributor 即可。

更新:对于 Wicket 1.5,请参阅迁移指南< /a>,但它是这样的:

public class MyPage extends WebPage {
   public MyPage() {
   }
   public void renderHead(IHeaderResponse response) {
     response.renderJavaScriptReference(new PackageResourceReference(YuiLib.class,
       "yahoo-dom-event/yahoo-dom-event.js"));
     response.renderCSSReference(new PackageResourceReference(AbstractCalendar.class,
      "assets/skins/sam/calendar.css"));
   }
}

如果您想将

JavascriptPackageResource.getHeaderContributor() does exactly what you need.

You need nothing in your markup, just add the HeaderContributor it returns to your page.

Update: For Wicket 1.5 see the migration guide, but it goes like this:

public class MyPage extends WebPage {
   public MyPage() {
   }
   public void renderHead(IHeaderResponse response) {
     response.renderJavaScriptReference(new PackageResourceReference(YuiLib.class,
       "yahoo-dom-event/yahoo-dom-event.js"));
     response.renderCSSReference(new PackageResourceReference(AbstractCalendar.class,
      "assets/skins/sam/calendar.css"));
   }
}

If you want to put your <script> element in the body, you can simply declare it as a WebMarkupContainer and add an AttributeModifier to set the src attribute. Although in that case wicket won't generate the relative URLs for you, you have to do it yourself.

你与清晨阳光 2024-12-25 14:45:04

我不确定我是否完全理解了。
如果您尝试在页面加载后创建脚本并将其附加到正文,您应该这样做:

 <script type="text/javascript">
    function load_js() {
    var element = document.createElement("script");
    element.src = "scripts/YOUR_SCRIPT_SRC.js"; // <---- HERE <-----
    document.body.appendChild(element);
}

    // Wait for the page to be loaded

if(window.addEventListener)
    window.addEventListener("load",load_js,false);
else if(window.attachEvent)
    window.attachEvent("onload",load_js);
else
    window.onload = load_js;

</script>

我在这里所做的是创建一个新的 script 元素,然后将其源应用到它。
这样您就可以动态控制src。之后我将其附加到正文中。

最后一部分在那里,因此新元素仅在页面加载后应用。

I'm not sure I understood completely.
If you are trying to create and append a script to the body after the page is loaded you should do it this way:

 <script type="text/javascript">
    function load_js() {
    var element = document.createElement("script");
    element.src = "scripts/YOUR_SCRIPT_SRC.js"; // <---- HERE <-----
    document.body.appendChild(element);
}

    // Wait for the page to be loaded

if(window.addEventListener)
    window.addEventListener("load",load_js,false);
else if(window.attachEvent)
    window.attachEvent("onload",load_js);
else
    window.onload = load_js;

</script>

What I did here is create a new script element, and then apply to it its source.
That way you can control dynamicaly the src. After that I append it to the body.

The last part is there so the new element is applied only after the page is loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文