View Full Version : Loading exnternal pages into div

08-27-2010, 05:43 PM
I've been searching the net for a few hours now for a js that i can use <a href="http://www.sitename.com/#content/home">Home</a> load into a div, but i cant seem to find one, anyone got any ideas? or good links to any.


Old Pedant
08-27-2010, 08:32 PM
Not a good idea.

A full HTML page will have <html> and <head> and <script> and <style> and <body> tags that will not work if plopped into a <div>.

You *might* get away with the <script> and <style> tags (but you might not! if they conflict with styles and script already in your page!) but the <html>, <head>, and <body> tags (and probably others) would be illegal and pure poison in the <div>.

You *might* be able to strip out everything from the page except the text *between* (but no including) the <body> and </body> tags, but it could also be pure luck if it works.

On top of all that, if the page uses JavaScript to do document.write() calls to create content, either that code will not be executed (so you don't get the content) or it will be executed, in which case the first document.write() will *WIPE OUT ALL EXISTING CONTENT* on the page!

Finally, you can *NOT* do this if the page you are attempting to load is in another domain. That's not allowed. Period.

You *COULD* load another site's page into a <frame> or <iframe>. You couldn't use JavaScript to inspect or alter the contents thereof (again, cross-domain scripting is not allowed), but at least you could load the page.

08-28-2010, 12:53 AM
No it not if you dont put them in the external page.

Old Pedant
08-28-2010, 01:35 AM
Well, sure... If *YOU* have control over all the external pages, then it's TRIVIAL!

var http = (window.XMLHttpRequest != null )
? new window.XMLHttpRequest()
: new ActiveXObject("Microsoft.XMLHTTP");
http.open( "GET", "urlOfSomeOtherPage.html", false );
http.send( null );
document.getElementById("SomeDiv").innerHTML = http.responseText;

(That's doing the load synchronously. You could also do it async, with only a tiny bit more work.)