Saturday, January 31, 2009

Using javascript to Include a html file inside another html file

When there is no server side functionality needed we create the whole site in plain html. Usually these sites have left/right side menu, top header, footer etc.. If the site grows in to 20, 30 pages, it will be a headache to do a simple change like changing footer text. We will have to change each page. If we were using a server side technology like PHP, JSP, etc.., we will have the chance to keep the common areas in the site in different pages and include these parts in each page using a “include” statements.




We can do the same thing with the plain old html and javascript.



But how?

First you need to create the site main layout using divs and give them unique ids.



<html>
<title>HTML Includes</title>
<script language="javascript" src="js/main.js"></script>
<script src="js/prototype.js" type="text/javascript"></script>
<script language="javascript" xml:space="preserve">
// <![CDATA[
Event.observe(window, 'load', loadCalls);
// ]]>
</script>
<link type="text/css" href="css/main.css" rel="stylesheet"/>
<body>
<div class="page-sizer">
<div class="header" id="header"></div>
<div><div class="leftMenu" id="leftMenu"></div><div class="content" id="content"></div></div>
<span style="clear:both;" />
<div class="footer" id="footer"></div>
</div>
</body>
</html>



Also you'r gonna need a javascript library like prototype, Jquary, YUI. You don't have to have the whole library to do this. You only needs to have the files to make an ajax call. I am going to use prototype for my example but using other libraries wouldn't make much difference. So what I simply do is to access each div element and fetch it's content from a separate file and fill them.




function loadCalls() {
//Load the pages one by one to the divs
loadPages('common-pages/footer.html','footer');
loadPages('common-pages/header.html','header');
loadPages('common-pages/leftMenu.html','leftMenu');
loadPages('content/default.html','content');

}
function loadPages(path,loadToId){
//IE cache the response when the request parameters are the same
//So We send a random parameter weith the request
var random = Math.floor(Math.random() * 2000);

new Ajax.Request(path+'?rand=' + random,
{
method:'get',
evalScripts: true,
parameters:{random:random},
onSuccess: function(transport) {
document.getElementById(loadToId).innerHTML = transport.responseText;
},
onFailure: function() {
setError('Problem with loading the file');
}
});
}

And from the menu file I am loading each page to the “content” div as follows.



<a onclick="loadPages('content/page01.html','content');">page1</a>

You can download the complete source code from here http://sites.google.com/site/chanaka3d/Home/html-include.zip?attredirects=0


Note: This method will not work in IE if you are not hosting it in a sever. But in Firefox it doesn't necessarily need a sever to work.

8 comments:

Society for Handicapped said...
This comment has been removed by the author.
Ben Kaeding said...

This does not seem very SEO friendly. Wouldn't the content of the pages be hidden from the search engines?

chanaka said...

Yes I agree. That's a downside.

Sneakers hobbies said...

we have lots of polo shirts
Abercrombie Fitch

clothing

Abercrombie Fitch

hoodile

ralph lauren shirts
Columbia

Jacket

north face jackets
spyder ski

jacket

polo shirts
polo shirt
spyder jackets
mens jacket
mens spyder jackets
spyder jackets for cheap
spyder ski jacket
womens spyder jackets
ralph lauren
spyder jackets cheap
spyder jackets for men
cheap spyder jackets
north face

jackets

discount spyder jacket
the north face

jacket

ralph lauren polo shirts on

sale

discount ralph lauren polo

shirts

ralph lauren shirts
ralph lauren polo discount
ralph lauren polo sale
ralph lauren

polo shirts

ralph lauren polo wholesale
north face jackets
north face jacket
Columbia

Jacket

ralph lauren jacket
Abercrombie Fitch hoodile
Abercrombie Fitch

clothing

abercrombie fitch
abercrombie

fitch


welcom to our store.

season said...
This comment has been removed by the author.
season said...

Your article is very good.I like it very much.
Once upon a time, there was a mouse father.He wanted to marry his daughter to the greatest person in the world.But, who was the greatest person in the world?Oh!
puma ferrari shoes
cheap nike shoesThe sun! He must be the greatest person in the world.The mouse father went to talk to the sun."Hello! Mr. Sun.
puma shoes
ferrari shoes
I know you are the greatest person in the world.Would you marry my daughter?""What? I'm not the greatest person in the world. The greatest person is the cloud.If he comes out, I’ll be covered."
nike shox nz
Ugg Boots
The mouse father went to talk to the cloud. “Hello! Mr. Cloud. I know you are the greatest person in the world. Would you marry my daughter?”
nike 360 air max
nike shox shoes
“What? I’m not the greatest person in the world. The greatest person is the wind.If he comes out, I’ll be blown away.”
cheap puma shoes
puma drift cat
The mouse father went to talk to the wind. “Hello! Mr. Wind. I know you are the greatest person in the world.Would you marry my daughter?” “What? I’m not the greatest person in the world. The greatest person is the wall. If he comes out, I’ll be stopped.”
cheap nike shox
nike air max 360
The mouse father went to talk to the wall. “Hello! Mr. Wall. I know you are the greatest person in the world. Would you marry my daughter?” “What? I’m not the greatest person in the world. The greatest person is YOU, the mouse.” “The greatest person in the world is … mouse?” “Yes, the greatest person in the world is mouse. See? If mouse comes out, I’ll be bit!”
nike air max
pumas shoes
The mouse father was very happy. He finally knew mouse was the greatest person in the world. He would marry his daughter to the handsome mouse next door.
puma mens shoes
nike shoxs
Chaussures puma
nike womens shoes
mens puma shoes
nike air max

theprophet said...

There was this guy who believed very much in true love and decided to take his time to wait for his right girl to appear. He believed that there would definitely be someone special out there for him, but none came.
Every year at Christmas, his ex-girlfriend would return from Vancouver to look him up. He was aware that she still held some hope of re-kindling the past romance with him. He did not wish to mislead her in any way. nike shoxsSo he would always get one of his girl friends to pose as his steady whenever she came back. Chaussures pumaThat went on for several years and each year, the guy would get a different girl to pose as his romantic interest. So whenever the ex-girlfriend came to visit him, cheap ugg bootsshe would be led into believing that it was all over between her and the guy. nike womens shoes The girl took all those rather well, often trying to casually tease him about his different girlfriends, or so, as it seemed! In fact, the girl often wept in secret whenever she saw him with another girl, but she was too proud to admit it. Still, every Christmas, she returned, hoping to re-kindle some form of romance. But each time, she returned to Vancouver feeling disappointed.
Finally she decided that she could not play that game any longer. puma mens shoes Therefore, she confronted him and professed that after all those years, he was still the only man that she had ever loved. mens puma shoesAlthough the guy knew of her feelings for him, he was still taken back and have never expected her to react that way. He always thought that she would slowly forget about him over time and come to terms that it was all over between them. nike shox Although he was touched by her undying love for him and wanted so much to accept her again, Nike Max Tn Chaussureshe remembered why he rejected her in the first place-she was not the one he wanted. nike 360 air maxSo he hardened his heart and turned her down cruelly. nike running shoesSince then, three years have passed and the girl never return anymore. They never even wrote to each other. NIKE air shoes The guy went on with his life..... still searching for the one but somehow deep inside him, he missed the girl.
On the Christmas of 1995, he went to his friend's party alone. "Hey, how come all alone this year? Where are all your girlfriends? What happened to that Vancouver babe who joins you every Christmas?", asked one of his friend.nike air max He felt warm and comforted by his friend's queries about her, still he just surged on.
Then, he came upon one of his many girlfriends whom he once requested to pose as his steady. He wanted so much to ignore her ..... not that he was impolite,wholesale nike shoes but because at that moment, he just didn't feel comfortable with those girlfriends anymore. nike shox torchIt was almost like he was being judged by them. The girl saw him and shouted across the floor for him. Unable to avoid her, he went up to acknowledge her.
"Hi......how are you? Enjoying the party?" the girl asked.
"Sure.....yeah!", he replied.
She was slightly tipsy..... must be from the whiskey on her hand.

bbeatifullife said...

I like your blog. Thank you. They are really great .
Some new style Puma Cat shoes is in fashion this year.
The Nike Air Shoes is a best Air Shoes .
Nike Air Rift is good and Cheap Nike Shoes.
If you are a fans of Puma basket,we would offer the good and Cheap Puma Shoes for you .the cheap ugg bootsis best christmas gift now.
The information age is really convenient .