0

I'm sorry if this is considered too general a question. I have a website that I'm creating from scratch and without any CMS or fancy tools. I have like 20 lines of HTML that compose the header that is the same on every page. So it seems logical that I should use document.write(header) inline where the header needs to be on each page, and before that have a way of grabbing the HTML for the header from a local HTML file.

  • Is this easy to do?
  • If so, what functions do I need?
  • Could you provide an example, assuming that the external file is called header.html?

2 Answers 2

1

You can do this using AJAX - on page load, insert the contents of some URL into a DIV. The problem with this technique is that it really sucks for SEO - crawlers will see that page without the loaded content.

NEVER use document.write because it stops page execution and can only be used effectively DURING load time, not AFTER.

Sign up to request clarification or add additional context in comments.

2 Comments

What function should I be using if not document.write?
This: api.jquery.com/jquery.ajax You use .html() or plain old .innerHTML() to set the value in an existing DOM element.
0

It's probably much better/easier if you look into templating languages such as Handlebars.js.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.