Search code examples
phpjavascriptincludehead

php include file containing both html markup and js scripts


A very simple question:

  • "Headers.html" contains a bunch of dropdown menus driven by js scripts. The scripts are in the head section, and the menus are in the body section.

  • "Content1.html", "Content2.html", etc. have their own < head> and < body>, and in addition they contain a PHP include statement within < body> that brings in the elements of "Headers.html".

This arrangement is illegal since it duplicates < head> and < body> in the resulting file, right? However, if I do not define < head> in "headers.html", I end up with js scripts in the middle of the body of the final file. I thought of putting all scripts into a third file which would be included within < head> of the content file, but that means that the menus in "headers.html" will not work as standalones, which is an annoyance. So, what is the best practice to include html files that contain scripts?


Solution

  • Keep scripts and markup strictly in separate files and include them where required.