add javasctipt to a template

Hi,

I have a template that i want to redefine when touching a button and in that new code i want to add some javascript.

Is this possible?

Thanks and greetings!

Hello,

Could provide more information about this issue ?

As you know template can be defined via JS function:

viewtopic.php?f=22&t=38077

  1. since you can’t use { or } (they are delimiters) you can use the built in {ldelim} (outputs a {) and {rdelim} (outputs a })

  2. You can wrap your javascript in {literal} tags:

Code:
{literal}
function myfunc() {
}
{/literal}

Smarty ignores everything between {literal} … {/literal}, so you would have to do the following if you are writing dynamic code:

Code:
{literal}
function myfunc() {
return ‘{/literal}{$myvar}{literal}’;
}
{/literal}

  1. If your script is simple and is static, you might be able to put it into a configuration file.

  2. If your script is static, you can collect it all into a .js file and use the src parameter to include the file.

If you still have any issue try to follow below steps which can help you to add JavaScript files to a template in much the same way as adding in a stylesheet with custom code.

Edit the template you want to add the JavaScript file to.
Click the Custom Code tab and ensure you are adding code into the PagePreload event.
Add the following code to the input area, then Save the template.
CurrentContext.Page.Scripts.Add("/SiteElements/js/scripts.js")

This call to the Web API will add your JavaScript files into the page just before the closing body tag to keep inline with best practices for loading script files. If more than one call to include the same file is registered, the file will only be included once.