2

In HTML is there such a line of code that will do the same thing as PHP's require_once? I'm just curious because there are some lines of codes that I want to duplicate through multiples sheets without needing to require myself to type it each page.

I know I can do it via PHP, but I am looking for an HTML variant? Is there such a beast or am I barking up the wrong tree?

Matt Ridge
  • 3,633
  • 16
  • 45
  • 63
  • 5
    HTML doesn't have *any* mechanism for doing includes. For that, you need SSI, PHP, or some other in-line scripting technology. Or you can slurp stuff in using JavaScript, and implement your require_once functionality there. But that's not HTML. – Graham Jan 27 '12 at 21:14
  • 1
    What's wrong with this question? – jeroen Jan 27 '12 at 21:20
  • 1
    There'd have to be a HTML equivalent of `require` first... – ceejayoz Jan 27 '12 at 21:24
  • 1
    I second jeroen's question: why the downvote for this question? -2 doesn't seem warrented. +1-ing to restore balance! – Ben D Jan 27 '12 at 21:33
  • ... though, I can see how some people might object to the use of the ambiguous word "sheets" and the use of `require_once` instead of `require` (which seems to be the question) – Ben D Jan 27 '12 at 21:39
  • @Ben D Downvotes should come with an explanation... – jeroen Jan 27 '12 at 21:51
  • Agreed; this is a question worth answering (even if the answer is you can't do what the OP wants), so I upvoted too. – Mr Lister Jan 27 '12 at 22:04
  • Sorry, I was at work posting this, and honestly I didn't mean sheet, I meant pages. I was working with a massive migrane and my mind was wondering all over the place. Thanks for understanding though. – Matt Ridge Jan 27 '12 at 23:02

4 Answers4

4

That depends on what you want to include. Including a PHP-File is not possible, if you want to include a CSS stylesheet, use:

<link rel="stylesheet" type="text/css" href="yourstylefile.css" />

and for a Javascript file

<script type="text/javascript" src="yourscriptfile.js"></script>

Of course you have to put that code between the header-tags.

Dion
  • 3,145
  • 20
  • 37
2

No, there is no include mechanism in HTML. Unless you count SSI.

Edit: wait, "sheets"? You mean CSS?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • No, sorry I meant pages, I was working long hours and had a migrane I couldn't remember the right terminology at the time. I meant pages, not sheets. – Matt Ridge Jan 27 '12 at 23:03
1

Yeah, SSI is the closest you're going to get. However, there are many non-server-side ways to get around this. Several web development applications have html templating systems that replicate server-side includes on the development side. For example, dreamweaver allows you to insert repeatable regions into HTML templates. When you modify the "included" file, Dreamweaver will modify all HTML files that use that block. As this is not a true include, but rather an HTML-updating system, you do have to then re-upload these files if you use a remote server, but if you have to stick to plain HTML it can make projects much more manageable and is much better than using iframes.

Lastly, there is also the option of having Javascript build a repeating block of code. You can simply include a common javascript library on every page <script type="text/javascript" src="templater.js"></script> and have it build the element on the client's side (either with an innerHTML call or inserting elements into the DOM). This has the obvious downside that

  1. It requires Javascript to work
  2. It might mess with SEO
  3. It may slow down page loads (from the client side anyhow)

Using a proper include in a server side language is of course the best approach, but in a pinch these are both potential alternatives.

Ben D
  • 14,321
  • 3
  • 45
  • 59
0

Technically you can create an iframe on your page which will load and handle a separate page but it does not function like include or require once. And to this I know of no alternatives.

HenryGuy
  • 254
  • 2
  • 9