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

Load HTML page into another

P: n/a
<html>
<head>
<script type="text/javascript">
function init(){
var html = document.open('Hello.html');
document.getElementById
}
</script>
<body onload ="init();">
<did = 'hi'>
</d>
</body>
</html>
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Duke wrote:
<html>
<head>
<script type="text/javascript">
* * * * function init(){
* * * * * * * * * * * * var html = document.open('Hello.html');
document.getElementById
Perhaps you meant:

document.getElementById('hi').innerHTML = html;
* * * * }
</script>
<headshould be finished here. W3 requires a doctype declaration and
a page title, too.
<body onload ="init();">
* * * * <did = 'hi'>
* * * * </d>
Perhaps you meant:

<div="hi">
</div>
</body>
</html>
The open-method can't be used here. XMLHttpRequest is meant for that
purpose.

Putting it all together:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Demo</title>
<script type="text/javascript">
var xhr;
function init() {
xhr = null;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}

if (xhr != null) {
xhr.onreadystatechange = state_Change;
xhr.open('GET', 'Hello.html', true);
xhr.send(null);
}
else {
alert('Your browser does not support XMLHttpRequest.');
}
}

function state_Change() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
document.getElementById('hi').innerHTML = xhr.responseText;
}
else {
alert('Problem retrieving data:' + xhr.statusText);
}
}
}
</script>
</head>
<body onload="init()">
<div id="hi"></div>
</body>
</html>

But there is an important objection! The content of the <divis now
actually a full HTML-page inside another one. Though browsers might be
"forgiving", it's still invalid and quite dangerous design. The
following variant is more healthy:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Demo</title>
<script type="text/javascript">
function init() {
frames['hi'].location = 'Hello.html';
}
</script>
</head>
<body onload="init()">
<iframe name="hi"></iframe>
</body>
</html>

Hope this helps,

--
Bart
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.