Search…

X3 Photo Gallery Support Forums

Search…
 
User avatar
Artur
Imagevue PowerPack
Topic Author
Posts: 510
Joined: 20 May 2011, 03:17

How to add my own js script?

07 Jan 2015, 14:38

When I try to add my own javascript in footer section (can't find better place in admin panel), the script seems to be fired ONLY when the page is loaded. After I navigate to another page, the script won't fire again.
I couldn't find any javascript method/listener to check if pathname has been changed (it's "workaround" but I could fire my script then).

Also, I would like to be able to use jQuery. As for now I access jQuery using the code below, but it's not really appropriate way.
Code
function if_jQ_is_loaded(myFunc) {
    if (window.$){
        myFunc();
    }else{
        setTimeout(function() { if_jQ_is_loaded(myFunc) }, 100);
  }
}
if_jQ_is_loaded(function () {
    	//blah blah blah...
});
I could update imageve files but it's quite uncomfortable because of updates.

Is there a way I could be abble to use jQuery without "workarounds" and add my script which will fire on each page?
 
User avatar
mjau-mjau
X3 Wizard
Posts: 13998
Joined: 30 Sep 2006, 03:37

Re: How to add my own js script?

08 Jan 2015, 00:38

What version are you using? we added an explicit "javascript" section in the panel -> custom/javascript/ since version 0.7 or 0.8, but since it is inside "content", I am aware some users do not get the custom "goodies" updated when they exclude "content". Download the latest release, and manually add the content/custom/javascript/ folder!

There is not a huge difference in adding scripts to the custom/footer as you did, but the custom/javascript section is tidier for, does not mingle with custom html, and includes some parsing so that you can add remote scripts. More importantly, it includes some basic API functions to call JS after jQuery is loaded (jquery is included in X3), as well as triggering JS on unique ajax 'page loads' ... As you may be aware, X3 is an ajax-application, and by default therefore, it would only trigger your custom javascripts on initial page load, but not when navigating the links.
Code
// X3 CUSTOM JAVASCRIPT
// This is where you add counters, stats and any other custom javascript.
// NB! Make sure your scripts are wrapped inside the <script>...</script> tag.
// function x3_load(){ console.log('Runs after x3 is loaded, jquery available.'); };
// function x3_load_page(){ console.log('Runs after each X3 ajax page load'); };

// EXAMPLE 1
// <script src='//cdnjs.cloudflare.com/ajax/libs/sizzle/2.0.0/sizzle.min.js'></script>

// EXAMPLE 2
/*
<script>
  function yourFunction(){
    console.log('log: wow my function!');
  }
  yourFunction();
</script>
*/
I implemented a custom javascript for one user, and you can see the code here for reference:
https://gist.github.com/mjau-mjau/eb7190f1d445fa5e71a0
 
User avatar
Artur
Imagevue PowerPack
Topic Author
Posts: 510
Joined: 20 May 2011, 03:17

Re: How to add my own js script?

08 Jan 2015, 06:41

Thanks for clarification.

I'm still on beta.v2 (or v3, don't remember exactly).
I would definitely try with the latest beta - I sent you a PM :wink:
 
User avatar
Artur
Imagevue PowerPack
Topic Author
Posts: 510
Joined: 20 May 2011, 03:17

Re: How to add my own js script?

10 Jan 2015, 16:10

All I needed working now nicely 8) Thanks