javascript 如何在javascript中加载本地网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11118066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:07:49  来源:igfitidea点击:

How to load a local web page in javascript

javascript

提问by synack

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

谢谢

采纳答案by Denys Séguret

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.htmldo

如果你想打开一个名为google.htmldo的本地页面

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.

这将确保可以加载链接的资源,因为相对路径将符合页面的位置。请勿自行更改内容。

回答by Rich O'Kelly

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.

这很可能是因为允许外部资源(图像、样式表、js 文件等)具有相对 url。任何相对 url 都需要完全限定页面才能正确加载。