I have 2 HTML files, suppose a.html
and b.html
. In a.html
I want to include b.html
.
In JSF I can do it like that:
<ui:include src="b.xhtml" />
It means that inside a.xhtml
file, I can include b.xhtml
.
How can we do it in *.html
file?
In w3.js include works like this:
using jquery u need import library
i recommend you using php
b.html
No need for scripts. No need to do any fancy stuff server-side (tho that would probably be a better option)
Since old browsers don't support seamless, you should add some css to fix it:
Keep in mind that for browsers that don't support seamless, if you click a link in the iframe it will make the frame go to that url, not the whole window. A way to get around that is to have all links have
target="_parent"
, tho the browser support is "good enough".You can do that with JavaScript's library jQuery like this:
HTML:
JS:
Please note that
banner.html
should be located under the same domain your other pages are in otherwise your webpages will refuse thebanner.html
file due to Cross-Origin Resource Sharing policies.Also, please note that if you load your content with JavaScript, Google will not be able to index it so it's not exactly a good method for SEO reasons.
Based on the answer of https://stackoverflow.com/a/31837264/4360308 I've implemented this functionality with Nodejs (+ express + cheerio) as follows:
HTML (index.html)
JS
append -> includes the content into the div
replace -> replaces the div
you could easily add more behaviours following the same design
PHP is a server level scripting language. It can do many things, but one popular use is to include HTML documents inside your pages, much the same as SSI. Like SSI, this is a server level technology. If you are not sure if you have PHP functionality on your website contact your hosting provider.
Here is a simple PHP script you can use to include a snippet of HTML on any PHP-enabled web page:
Save the HTML for the common elements of your site to separate files. For example, your navigation section might be saved as navigation.html or navigation.php. Use the following PHP code to include that HTML in each page.
Use that same code on every page that you want to include the file. Make sure to change the higlighted file name to the name and path to your include file.