Load HTML page into another

D

Duke

<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>
 
B

Bart Van der Donck

Duke said:
<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>

<body onload ="init();">
        <did = 'hi'>
        </d>

Perhaps you meant:

<div="hi">
</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 <div> is 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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top