HTML Import advisable to use? Or any other alternative?


#1

Since Mozilla doesn’t support HTML import and I guess we have to enable it explicitly from some where, is it still a good idea to use HTML import to include external HTML files in the main HTML file?

I am just trying to figuring out what’s the best way to include an external HTML file inside a HTML file since my code is growing faster.

If HTML import is not a good way to go, could anyone please suggest another efficient way to accomplish this?

Thanks !


#2

Hi Jack_Tauson_Sr,

One way to include things like an external file in an html page is to rename the file extension to .shtml and use Server Side Includes (SSI) in the html.

More info:
https://www.htmlgoodies.com/beyond/webmaster/article.php/3473341/SSI-The-Include-Command.htm

A tutorial:
http://www.javascriptkit.com/howto/ssi.shtml

This was the standard method before PHP became common.
Please mind that the external file to be included, SSI, PHP or whatever, should only consist of the code that would be in that place as a part of the html. The file with the SSI command must have the shtml extension, but the file to be included could have any extension you like though most people use html so the file can be viewed by its own,

(Please don’t mix up includes with frames/iframes where a complete html file is displayed but not a part of the html that contains the frame only.)


#3

Doing things server side should be more robust, being browser agnostic.
PHP is freely available and offered as standard by most hosts.
It can be used for things as simple as basic includes, but will also offer the ability it include a little logic too, or a whole world of possibilities beyond that as the site grows and your needs become more complex.


#4

Thanks. I believe I would not be able to test this approach locally? Do you think there’s something more modern that I could use.

Actually, I converted all PHP files recently to HTML and looking for a way to include the external files just like PHP has include function. I would prefer not to go to something which was used before PHP arrived.


#5

We just moved away from PHP and hence not planning to use PHP : (


#6

If all you’re looking for is to include one HTML file within another then SHTML would make sense a it’s part and parcel of Apache.

I must say it seems rather odd to ditch PHP before finding a replacement.


#7

Thanks. Actually, I am not planning to use any server side related thing, hence SHTML wouldn’t be a good option for me as well. I believe, I would have to use the jQuery load everytime I have to use external HTML as mentioned in the post below:

<script> 
    $(function(){
      $("#includedContent").load("b.html"); 
    });
    </script> 

If I have to include multiple HTML files at different locations, does the above approach sounds reasonable?


#8

JS seems a strange choice for including a file in another. It could well give users a jumpy experience as the included file is actually loaded. And what about users who have disabled JS?


#9

I see, the SSI requires the external files to be on the same server, but you want to import files that is located elsewhere on the web.

I didn’t include that option reading your question. :wink:

There doesn’t seem to be much else to do until browser vendors come to an agreement on html imports.
(Edit) Except using PHP includes of course. :stuck_out_tongue:

Though that doesn’t say you can’t use SSI for files that you can place on the same server, I think it could be a good combination.

Otherwise users like me that are restrictive with javascript wont see any of your includes. :slight_smile:


#10

Thanks again. Your answer does answers my question. When I say “to include HTML file from somewhere else”, I mean from different directory and not somewhere else from the web. Sorry about the confusion.

That’s where I was thinking of using the jQuery load method as explained in the post I shared above. Thanks


#11

Moved the topic to “General Web Dev”, if you don’t mind. :slight_smile: