0

One of my main challenges right now is figuring out an elegant way to create an HTML 'module' in one file that will deploy to all of my websites (i.e. copyright language, site navigation, EEO statement, etc.) But I'm aware that HTML import is still being developed and is only available to those who opt-in on Chrome 36 or later.

Is there a solution using JavaScript where I can create a string that contains HTML and deploy it as an organic part of my pages? And what about doing the same for CSS in a way that will be browser compatible (http://webdesign.about.com/cs/css/qt/tipcssatimport.htm)?

Here's sample code:

<'ul>
  <'li><'a href="#">Learn to Code<'/a><'/li>   
  <'li><'a href="#">Current Projects<'/a><'/li>
  <'li><'a href="#">Future Projects<'/a><'/li>
  <'li><'a href="#">Get in Touch<'/a><'/li>
<'/ul>

I know that I could redo everything using JavaScript but it'd be very nice to continue working in HTML or something that intuitively feels like it.

  • 2
    When you start needing that kind of stuff, it's a sign that you need a server-side language that will generate HTML on the fly. PHP is said to be one of the easiest to start with. – Sergio Tulentsev Apr 26 '15 at 14:24
  • First choice would be server side includes. Otherwise can use ajax but note that search engines may not see your links – charlietfl Apr 26 '15 at 14:24
  • I'm always impressed with the knowledge on this forum - thanks for the help. Sergio (and others), what would be the benefit and drawbacks of using PHP as opposed to Python. Learning Python is a future goal but I don't know what bulk there might be in terms of installing extra packages on my server or locally when I write the code from various desktops. Any insights? I know they're similar. – EnglishTeacherEric Apr 26 '15 at 16:13

1 Answers1

0

Is there a solution using JavaScript where I can create a string that contains HTML and deploy it as an organic part of my pages?

document.write in an external <script> will do that. It is pretty horrible though, and you'd be better off using either a build time template system or a server side include or template system.

And what about doing the same for CSS in a way that will be browser compatible

@import and <link> are well supported by browsers old and new. The article you link to warns about "Netscape 4.x and IE 3.x" which are ancient and not in any significant use this decade.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335