|
From: Duke on 18 Apr 2008 07:08 <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>
From: Bart Van der Donck on 20 Apr 2008 04:43 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> <head> should 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 <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, -- Bart
|
Pages: 1 Prev: inner classes proble Next: Load HTML page into another HTML page using Javascript |