Forums / Cotonti / Development / Loading custom JS script in plugins

Tip

ez
#1 2010-02-26 05:26
I found this usefull.. and will share it with you :)

If you need a custom js script for a plugin, DO NOT load it always in the header.tpl...
OR {HEADER_COMPOPUP} tags, you CAN load jquery plugins or other scripts only when you really need them with the following code:

function addJavascript(jsname,pos) {
	var th = document.getElementsByTagName(pos)[0];
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',jsname);
	th.appendChild(s);
}

look at: Link to article

So keep your code clean, and only load stuff when needed.. B)
==- I say: Keep it EZ -==
Trustmaster
#2 2010-02-26 06:40
Yes, thanks for popping it in our minds! There's also a jQuery.getScript() function, which uses AJAX to load files.

We'll try this solution in trunk and see how it works.
May the Source be with you!
ez
#3 2010-02-26 16:03
Didn't know that one yet... thnx

You can also load additional css (e.g. for plugins)
$.ajax({
  url: url,
  dataType: 'script',
  success: success
});

thnx trustmaster
==- I say: Keep it EZ -==
This post was edited by ez (2010-02-26 16:22, 14 years ago)