Answer the question
In order to leave comments, you need to log in
How to execute js only on certain pages?
Here everyone glues all the js into a single minified file.
And how to execute its pieces only on certain pages?
After all, on each page its functions are called, its widgets, its events are initialized.
It seems that it's okay - after all, if there are no elements, then nothing is initialized, but ... What is it? Will all the code be re-executed every time the link is clicked (assuming we don't have a spa)?
Answer the question
In order to leave comments, you need to log in
And in this matter it will help you for example:
requirejs.org
browserify.org
What does it mean to be initialized?
Initializes nothing if selector: [data-toggle="modal"]
none
That is, only the selector is searched.
Or are you talking about some other initializations?
It's a weird question, but I'll try to answer.
Example from backend in php. In the template, you make a script tag, you pass it from the controller, or whatever you have there is the name of the page.
<script>
var currentPage = <?=$this->pageName?>; // Например $this->pageName == "first"
</script>
var pages = {};
pages.first = function() {
// тут код который должен вызываться на первой странице.
}
pages.second = function() {
// тут код который должен вызываться на второй странице.
}
// потом вызываете код нужной страницы.
pages[currentPage]();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question