Is it considered good practise to have jQuery scripts in a PHP conditional? -


I have many jQuery scripts that are used on my website, but not on every page. I keep these scripts in a PHP condition so that they can output them only to the required pages.

Now I am wondering: Is this considered a good practice? What is the speed of this website? Are there any positive aspects to using this method?

  & lt ;? Php if (is_home ()) {? & Gt; & Lt; Script & gt; // jQuery Code // jQuery Code // jQuery Code & lt; / Script & gt; & Lt ;? Php}? & Gt;  

However nothing wrong By doing this you are giving browser-side caching to the smallest temporary feature. I say temporarily because it is definitely nothing that I will deploy on a large scale system.

Due to browser-side caching is being given because a PHP file always requests a fresh request from the browser because it is considered dynamic data.

I recommend a /assets/js/pages/home.js layout for your JS files which are conditionally included as an external file because Be more scalable in the future:

  & lt ;? Php if (is_home ()) {? & Gt; & Lt; Script src = "/ assets / js / pages / home.js" & gt; & Lt; / Script & gt; & Lt ;? Php}? & Gt;  

Comments