Loading js file for a single page, best practice? #143
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hello there,
I cannot find the best solution for loading js files, only for a single page and only once.
I've divided all my js files between global files and single-page files, respectively outside and inside the pjax container that is being replaced.
But of course every time I come back to a single page the js file is loaded again and all the function like
on.('click')are fired another time.What is the best practice here?
I would prefer not to load all js files at once.
Thank you
Best
nbl7
Can you explain a bit more what your setup looks like, or show some code? I'm not sure what you mean by single page file.
Yes, sorry.
For example, this is the map of a website:
All three pages need
jquery.jsandmain.js.But Homepage needs another script called
home.jsand Contact needs
contact.js#mainis the container I am replacing withpjaxIf I put the script
home.jsinside the div#main, every time i go to the homepage thehome.jsis fired, so I don't think is the right way of handling the js.What would be the best practice to load only once
home.jsandcontact.js?Thank you
Best
nbl7
I can think of 2 options:
<head>of your container page, store any data in global variables, and access them from the corresponding page.Thanks for the answer.
That is what I did. But I thought there was a way the plugin can automate this process.
The reason I don't wan't to load twice the js is because otherwise I will bind multiple times click events on DOM elements.
Thanks
nbl7