By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,628 Members | 1,696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,628 IT Pros & Developers. It's quick & easy.

XMLHTTP object and client-side include

P: n/a
I'm trying to develop a way to include static files in htm pages with
javascript.

I'm trying to use XMLHTTP object this way:

----------------------------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>XMLHTTP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

var xmlhttp=false;

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

function include(xUrl,xId){
xmlhttp.open("GET", xUrl,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.getElementById(xId).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null)
}

</script>
</head>
<body>
<script type="text/javascript">
include("header.htm", "xheader");
</script>
<div id="xheader"></div>
<hr>Corpo<hr>
<div id="xfooter"></div>
<script type="text/javascript">
include("footer.htm", "xfooter");
</script>
</body>
</html>

----------------------------------------------------------------------
header.htm and footer.htm are html examples files.

It doesn't work if I call header and footer but if I call only header it
works.
Any help much appreciated.

Best Regards.
--
Fabri
(Non tuttu oru chiddu chi stralluci, n tuttu veru chiddu ca si dici)
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Fabri wrote:
I'm trying to develop a way to include static files in htm pages with
javascript.

I'm trying to use XMLHTTP object this way:

----------------------------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>XMLHTTP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

var xmlhttp=false;

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

function include(xUrl,xId){
xmlhttp.open("GET", xUrl,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.getElementById(xId).innerHTML =
xmlhttp.responseText;
}
}
xmlhttp.send(null)
}

</script>
</head>
<body>
<script type="text/javascript">
include("header.htm", "xheader");
</script>
<div id="xheader"></div>
<hr>Corpo<hr>
<div id="xfooter"></div>
<script type="text/javascript">
include("footer.htm", "xfooter");
</script>
</body>
</html>

----------------------------------------------------------------------
header.htm and footer.htm are html examples files.

It doesn't work if I call header and footer but if I call only header it
works.
Any help much appreciated.

Best Regards.

why not simply use Server Side Include, or the php equivalent ?
Jul 23 '05 #2

P: n/a
Martin! wrote:
why not simply use Server Side Include, or the php equivalent ?

Because on my hosting-server they are not supported.

--
Fabri
(Non tuttu oru chiddu chi stralluci, n tuttu veru chiddu ca si dici)
Jul 23 '05 #3

P: n/a
Bonjour Fabri <no@sp.am> qui nous a crit :
[...]
function include(xUrl,xId){
xmlhttp.open("GET", xUrl,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.getElementById(xId).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null)
}

</script>
</head>
<body>
<script type="text/javascript">
include("header.htm", "xheader");
</script>
<div id="xheader"></div>
<hr>Corpo<hr>
<div id="xfooter"></div>
<script type="text/javascript">
include("footer.htm", "xfooter");
</script>
</body>
</html>
[...]


be carefull, you must place "div" tag before the script, ie :

...
<div id="xheader"></div>
<script type="text/javascript">
include("header.htm", "xheader");
</script>
...

--
Cordialement, Thierry ;-)

Jul 23 '05 #4

P: n/a
Fabri wrote:
Martin! wrote:
why not simply use Server Side Include, or the php equivalent ?


Because on my hosting-server they are not supported.


Then you should switch ASAP to another service provider or wait for, hm,
another year to get XMLHTTPRequest widely implemented. And then it is
likely that you have a Web where exploits let emerge UAs where this single
feature, short of disabling client-side scripting completely or not even
have it supported in the first place, can be disabled. Fortunately,
hosting prices would have dropped by then as well.

BTW: Your sender address is forged which violates/disregards Internet
standards, most certainly the Acceptable Use Policy of your Usenet access
provider (NGI S.p.A.) and Netiquette. Change this ASAP or prepare to face
the consequences.
PointedEars
--
Der Beweis ist ein Gtze, vor dem der Mathematiker sich foltert.
-- Sir Arthur Stanley Eddington (1882-1944)
Jul 23 '05 #5

P: n/a
Thomas 'PointedEars' Lahn wrote:
Fabri wrote:

Martin! wrote:
why not simply use Server Side Include, or the php equivalent ?
Because on my hosting-server they are not supported.

Then you should switch ASAP to another service provider or wait for, hm,
another year to get XMLHTTPRequest widely implemented.


It is supported in perhaps 90% of browsers on the web, how much more
"widely" do you want it implemented?

<snip>
BTW: Your sender address is forged which violates/disregards Internet
standards, most certainly the Acceptable Use Policy of your Usenet access
provider (NGI S.p.A.) and Netiquette. Change this ASAP or prepare to face
the consequences.


Please tell me we are not going to go through this childish juvenile
bullshit again about email addresses?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #6

P: n/a
You could also consider using iframes. They have their own set of
issues, but they are simple to implement.

Jul 23 '05 #7

P: n/a
In article <2L********************@comcast.com>, Hi************@aol.com
enlightened us with...

Please tell me we are not going to go through this childish juvenile
bullshit again about email addresses?


His childish bullshit about email addresses and attributions is why he's one
of only two people in my killfile. He spends more time going on about what
other people did wrong in their postings than any other topic. It negates the
useful stuff he posts.

Some people need to learn that they aren't the 'net police. Until they do, I
don't need to take up my valuable time reading their crap.

/ wishes there was a way to kill responses to this shit without killfiling
the people relying...

--
--
~kaeli~
If a turtle doesn't have a shell, is he homeless or naked?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.