Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a local web page in javascript

Tags:

javascript

I'm trying to load a local web page executing:

var html = document.open('google.html');
document.documentElement.innerHTML = html;

It loads the page but it's not well formatted and images won't display. How could I load the entire contents?

Thanks

like image 479
synack Avatar asked May 02 '26 22:05

synack


2 Answers

Most likely this is because external resources (images, style sheets, js files etc) are allowed to have relative urls. Any relative urls need to be fully qualified for the page to load properly.

like image 149
Rich O'Kelly Avatar answered May 04 '26 11:05

Rich O'Kelly


If you want to replace the content of the page by another one, simply do

document.location.href="http://google.com";

If you want to open a local page named google.html do

document.location.href=document.location.href.splitOnLast('/')[0]+"/google.html";

This will ensure that linked resources can be loaded as the relative paths will be accorded to the location of the page. Don't change the content yourself.

like image 30
Denys Séguret Avatar answered May 04 '26 10:05

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!