0

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?

2
  • why is it a huge annoyance to include the scripts file anywhere you need to use the menus? Jul 28, 2012 at 7:14
  • It is not a huge annoyance, but I was wondering whether it is "best practice" to lump together scripts and markup.
    – aag
    Jul 28, 2012 at 19:03

1 Answer 1

1

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

4
  • Agreed. However, there are side effects. If the included file refers to a script file by relative path, the path may no longer be valid in the including file. Short of using absolute paths, I do not see how this can be logically resolved.
    – aag
    Jul 28, 2012 at 19:05
  • @aag So these include files become something like a library. If you really cannot define some kind of simply file system layout for your files (why not?) then try to solve it on server side: use rewrite directives to simulate some kind of virtual file system layout that might be different for each project you use those files in.
    – arkascha
    Jul 28, 2012 at 19:37
  • One possible strategy may be to have (1) the include files, (2) the script files, and (3) the content files all 1 level down the root. Or, if more than 1 level, then all 1 level down one common directory. This way, relative reference to the Script directory from the Content directory will be the same as from the Include directory. Is my thinking straight or garbled?
    – aag
    Jul 29, 2012 at 5:26
  • @aag I don't see what is meant to be in the 'include files directory'. I'd think that the script files are to be included in content files. Are there other files I don't know about? Since your folder layout is static each file to be included has a static position. So you can reference it from any file in a static manner, relative or absolute. That reference only depends on the position of the including file, in case of a relative reference. All you have to take care about is not to push your including files around... I guess I'd have to see an example of your files to understand the problem...
    – arkascha
    Jul 29, 2012 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.