1

I'm working on a navigation bar similar to this website and google, and lets say i would like to make more sites with the same layout. How would i go about doing this with html? I know that php has:

<?php include  "file name"; ?>

Is there a way to do something like that in HTML document?

LittleB0Y
  • 124
  • 1
  • 8

4 Answers4

1

HTML is not a server-side language, it's just a markup language. It does not offer any way to include the contents of a different page in your HTML code. HTML simply doesn't support it.

If you're willing to use a library such as jQuery, then this will be an easy task. You can use the jQuery .load() method to fetch the contents of your HTML file and then inject it the HTML DOM.

$(document).ready(function() {
    $('#someDiv').load('/path/to/file.html');
});

Note that remote files won't work because of the Same Origin Policy. Also, note that it's a bad idea if you care about SEO. The search engine spiders won't see your actual HTML code, and they might miss out important parts of your website.

The best way to do this would be to use a server-side language. If you're already serving dynamic content, then it makes more sense to do so. Server-side scripting runs on the server machine and then the results of that scripting, generally HTML markup are then sent out to the client. The difference is that the HTML markup is generated before pageload, not during or after, as is the case with the method shown above.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • First, thanks for help. I don't want to do it in frame because i have to set a fixed width for it etc, and this may couse some issues, also i have my styles for that already. How would i go about doing it the java? Code example? – LittleB0Y Feb 22 '14 at 04:07
  • @littleboy I'd suggest reading up on JavaScript prior to implementing this, all you would get here would be a basic method on how to do this, you seem to be a novice to programming hence the basic question.. So my advice is research into languages prior to using snippets – Daryl Gill Feb 22 '14 at 04:25
  • I am quite new :) Just started college, but all i wana know now is how to load up a file into html. I will get to js in more detail when the time comes. – LittleB0Y Feb 22 '14 at 04:35
  • @DarylGill is right. You should read up more on the subject. I've updated the answer to include a short code snippet (using jQuery library) though. Hope that helps! – Amal Murali Feb 22 '14 at 04:36
  • Server side language? PHP for example? Also can i still use HTML tags in php file? – LittleB0Y Feb 22 '14 at 04:54
  • @LittleB0Y, see my answer for using PHP and HTML together. – David Feb 22 '14 at 05:05
  • @LittleB0Y: Of course, that's the whole point of PHP. PHP is a templating language, stuff like [this](http://pastebin.com/HLbWc8vf) is totally valid (and actually used in applications). – Amal Murali Feb 22 '14 at 05:10
  • Great, php it is, also does validation work on php files? Do i have to include doctype html in the file or do i put something else in it? What is the proper way to validate a php file with etc in it? – LittleB0Y Feb 22 '14 at 05:14
  • @LittleB0Y: It's the same as HTML files. The only difference is that the code inside `` tags will be parsed by the PHP language parser and the generated HTML markup (if any) will be rendered to the page. So, basically anything you can otherwise do with an `.html` page is possible with a `.php` page. I suggest you go through [this HTML primer](http://www.htmlgoodies.com/primers/html/article.php/3478131) and start learning PHP. I understand you're probably confused now, but read along and things will start to make sense gradually. Cheers! – Amal Murali Feb 22 '14 at 05:22
  • Haha, yes i am. All i leaned in college so far is HTML and css. So if i make the site i do everything exactly the same if i was doing it in the html extension except if i want to use php i use or something like that, or maybe it was just – LittleB0Y Feb 22 '14 at 05:27
  • One can't be expected to know everything right away and it takes time. Even though the Internet is "right there in front of us", doesn't mean it's been embedded in our DNA at birth. It took Amal, myself and many others, a great deal of time and effort to learn the markup languages etc. and only through trial and error and good tutorials, will you learn the do's and don'ts @LittleB0Y Try to put PHP with PHP, and HTML with HTML as much as possible, while avoiding mixing them up too much, that... comes later. – Funk Forty Niner Feb 22 '14 at 05:27
  • 1
    @LittleB0Y: ` – Amal Murali Feb 22 '14 at 05:29
  • So what i got from this is: PHP is basically a dynamic language version of HTML, and it does not need doctype UNLESS it has HTML in it. Alright thank you all for help. Also @Fred-ii- i would rather just use .php if it has everything and more then HTML has, then using ur method for extra work. Thanks for help though. – LittleB0Y Feb 22 '14 at 05:38
  • You're very much welcome. And what you wrote above is correct. However, it's usually best to place PHP above HTML. @LittleB0Y Enjoy the adventure :) – Funk Forty Niner Feb 22 '14 at 05:40
  • Oh one more question, If im going to include a .html file with the navigation code into index.php do i have to declare the doctype in the navigation file? – LittleB0Y Feb 22 '14 at 05:43
  • @LittleB0Y: I usually include them in the main file itself. – Amal Murali Feb 22 '14 at 06:19
  • @Amal:Ok i am having some... issues with this. Here is the link with my code it is not working, what am i doing wrong? When i press f12 in chrome it shows the php code in html comment like so – LittleB0Y Feb 22 '14 at 06:24
1

"Is there a way to do something like that in HTML document?"

Yes there is an HTML equivalent (if I can say) to include files, and it's called Server-side Includes (SSI) which uses the .shtml file extension, however this is an Apache feature. Yet, from a recent finding, it can run on Windows servers. Consult Microsoft's Developer Network for more information on the usage of SSI and this page also on Microsoft's IIS.net.

The syntax is: (no space between the <!-- and the #include...)

<!--#include virtual="/includes/file.shtml" -->

This would look and fetch a file called file.shtml to the root and inside the "includes" folder.

Note: This can be any folder you want it to be.

You can also include different file extensions such as .htm .html .txt and even another .shtml file.

I manage a few Websites with that particular file extension, and the benefit of including .shtml files, is that you can also do more includes inside those, but not with the other file extensions I already listed.

However, there is an exception to this rule. You can tell the server to treat .html or .htm to run as .shtml just as long as you use the

AddHandler server-parsed .html
AddHandler server-parsed .shtml
AddHandler server-parsed .htm

Apache commands inside an .htaccess file and placed in the root of your server.

Back to the matter at hand. You can use it anywhere in the document you wish to include a file in.

For more information on Server-side Includes and other available options, visit the Wikipedia Website, or Google the term "Server-side Includes (SSI)".

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • SSI includes can be a major pain since they're dependent on Apache in particular. Plus, the HTML markup won't be "*pure*" any more. **Don't use SSIs, just use a server-side language instead**. – Amal Murali Feb 22 '14 at 04:40
  • True, however all the servers I work on use Apache. Good point Amal, I will make a note of that in my answer. The OP asked *"How would i go about doing this with html?"*, so was giving the OP the option of using SSI. @AmalMurali – Funk Forty Niner Feb 22 '14 at 04:51
  • I just found this article on Microsoft's Website: http://msdn.microsoft.com/en-us/library/ms525185%28v=vs.90%29.aspx @AmalMurali and also http://www.iis.net/configreference/system.webserver/serversideinclude – Funk Forty Niner Feb 22 '14 at 04:54
  • Appreciate ur effort but im going to wait wait for Amal to answer :) – LittleB0Y Feb 22 '14 at 05:03
  • However, I'm not agreeing 100% with what you wrote in your answer: *"It does not offer any way to include the contents of a different page in your HTML code."* It should be reworded. @AmalMurali – Funk Forty Niner Feb 22 '14 at 05:03
  • You're welcome. And wait for Amal to answer what, something you wrote in his answer? @LittleB0Y And I've been working with `.shtml` files for many years. Just so you know. All they are is an HTML file, with a simple `s` tacked on to the file extension. Your server MOST probably supports it. After all, we're 2014, and not 1992. ;-) Plus, Javascript is not required to run `.shtml` files. If the user disables JS, you're back to square one. I'm just giving you a tried and true option. The choice is yours ;-) – Funk Forty Niner Feb 22 '14 at 05:04
  • Never said ur method doesn't work. I just want the easiest way to do this. – LittleB0Y Feb 22 '14 at 05:08
  • Easy always comes at a price. The word(s) I think you're looking for is "manageable" and "functionality" and "cross-browser" capabilities which SSI is. There's no add-on to use, it just runs. @LittleB0Y – Funk Forty Niner Feb 22 '14 at 05:09
  • @Fred-ii-: "It does not offer any way to include the contents of a different page in your HTML code" -- I stand by that. HTML doesn't support this. SSIs are only work-arounds, really. – Amal Murali Feb 22 '14 at 05:12
  • Then why have they been calling it "server-side includes" for the longest time? Have you not worked with SSI before? I've been doing it for 15 years. When I first heard I could "include" files way back when, in using SSI, I freaked and jumped for joy! lol I just can't understand why you're still saying that you can't include the content of different pages in HTML code, that's entirely false. @AmalMurali you will need to elaborate on that for me. – Funk Forty Niner Feb 22 '14 at 05:16
  • K thanks alot for getting so involved, i think i will just use .php instead to make it all easy. The only reason i posted this question is because i didn't know i can use html in php file and i didn't know how to code in php :) – LittleB0Y Feb 22 '14 at 05:18
  • I agree that HTML doesn't support this, but SSI does and that's what I wrote, I didn't say HTML, I wrote SSI. @AmalMurali – Funk Forty Niner Feb 22 '14 at 05:18
  • You're welcome. I thought you were looking for another way besides PHP to include files. That's the impression I got from your question. @LittleB0Y The important thing here is, you found your solution. – Funk Forty Niner Feb 22 '14 at 05:19
  • Since ur so active could u answer the question i commented on Amal's answer? – LittleB0Y Feb 22 '14 at 05:22
  • Your question about validating `` etc.? @LittleB0Y – Funk Forty Niner Feb 22 '14 at 05:24
  • @Fred-ii-: I think I'm sort of miscommunicating here. I'm not sure which part of this is unclear/incorrect/needs rewording: "HTML is not a server-side language, it's just a markup language. It does not offer any way to include the contents of a different page in your HTML code. HTML simply doesn't support it." -- By "It" in the second statement, I was referring to HTML, which by the official W3C specification doesn't support "include"ing contents from other files. – Amal Murali Feb 22 '14 at 05:27
  • 1
    It's Ok Amal, I understood what you meant :) @AmalMurali my mistake. – Funk Forty Niner Feb 22 '14 at 05:28
1

Just make your HTML files run through a PHP parser, I do that for all my sites so things like menus and other repeating sections can just be updated once. You do this with your .htaccess file.

This will make all .htm and .html files be run through the PHP parser as well.

<FilesMatch "\.(htm|html)$">
    SetHandler application/x-httpd-php
</FilesMatch>

Then you just use the same method you mentioned in your original post. Whatever you include will be processed by the PHP parser, then sent to the browser, which will interpret the HTML.

Note that PHP files can hold anything in them, only the parts inside <?php ?> tags will actually be processed by the PHP parser. So I have section that are all HTML, except for one small section that has something like a year. The below is how my websites will never have their copyright be out of date, even if the website is not maintained.

<p>
    Copyright<sup>&copy;</sup> NAME. All Rights Reserved. <?php echo date("Y"); ?>.
</p>
David
  • 4,744
  • 5
  • 33
  • 64
  • So paste this code in my .htaccess and each time the html file will refresh in the browser it will be recognized as php so the – LittleB0Y Feb 22 '14 at 05:06
  • @LittleB0Y: No, it will not. If you want to use PHP, use PHP. The [`SetHandler`](http://httpd.apache.org/docs/2.0/mod/core.html) directive forces all matching files to be processed by a handler. In this case, the `FilesMatch` directive will match all the HTML files, and all those will be processed as PHP instead. – Amal Murali Feb 22 '14 at 05:17
  • @LittleB0Y, Amal is correct, but it's not bad. All it does is associate a different file extension with PHP, and if all you files are including something, then this is the best way. Let HTML be .html, and PHP be .php. If you need some PHP in your HTML, it's still HTML, so use .html (Or .htm) – David Feb 22 '14 at 05:21
-2
<frameset cols="25%,50%,25%">
  <frame src="www.google.com">
  <frame src="www.facebook.com">
  <frame src="www.something.ocm">
</frameset>

FRAMETAG is not supported in HTML5! Beaware

In W3C Validation it will throw an error, but in real it will work.

user2751638
  • 47
  • 1
  • 6