4

I am using sitemesh for a spring based site. The problem is that I have some javascript that I want it to run on the onload event of only one specific page using jquery $(function() { ... }) and not on every page.

I have included the jquery.js in the bottom of my decorator, after the body. So, if I try to include a <script> tag in the body of my decorated page the script won't be executed because jquery will be loaded after that! I know that I could include the jquery.js in the header of my decorator so it will be before the custom script in the decorated page however I don't really like that solution since it will contain javascritp in the head of my page.

So I would like to have something like a placeholder in my sitemesh decorator in where the custom from my decorated page will be placed (as you can understand I come from the django world :p). Is this possible ? Do you propose anything else or should I just put my jquery.js in the header and be done with it ?

Serafeim
  • 14,962
  • 14
  • 91
  • 133

1 Answers1

8

To answer my question, after some search I found the following solution:

I Added the following to the end of my decorator page (after including jquery.js)

<decorator:getProperty property="page.local_script"></decorator:getProperty>

I also added the following

<content tag="local_script">
<script>
  $(function() {
    alert("Starting");
  });
</script>
</content>

The decorated result page contained the contents of the local_script tag exactly where I wanted them and everything worked fine :)

I don't know why this feature of sitemesh is not properly documented - using this you can have a great templating behaviour (like django).

Serafeim
  • 14,962
  • 14
  • 91
  • 133
  • 1
    You saved my day. Works perfectly. I was working on performance optimization and was not able to move local scripts to end of body. – Rhythm Ruparelia Apr 21 '19 at 09:20