Javascript 从javascript加载另一个html页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2018567/
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-08-22 22:25:26  来源:igfitidea点击:

Loading another html page from javascript

javascripthtml

提问by Pranav

Is it possible to load a Javascript program from an html page and then make the Javascript load another html page instead of the page the loaded the program?

是否可以从 html 页面加载 Javascript 程序,然后让 Javascript 加载另一个 html 页面而不是加载程序的页面?

回答by Grant Paul

Yes. In the javascript code:

是的。在javascript代码中:

window.location.href = "http://new.website.com/that/you/want_to_go_to.html";

回答by Ravia

You can include a .js file which has the script to set the

您可以包含一个 .js 文件,其中包含用于设置

window.location.href = url;

Where url would be the url you wish to load.

其中 url 将是您希望加载的 url。

回答by Alexander V. Ulyanov

Is it possible (work only online and load only your page or file): https://w3schools.com/xml/xml_http.aspTry my code:

是否可能(仅在线工作并仅加载您的页面或文件):https: //w3schools.com/xml/xml_http.asp试试我的代码:

function load_page(){
qr=new XMLHttpRequest();
qr.open('get','YOUR_file_or_page.htm');
qr.send();
qr.onload=function(){YOUR_div_id.innerHTML=qr.responseText}
};load_page();

qr.onreadystatechange instead qr.onload also use.

qr.onreadystatechange 代替 qr.onload 也使用。

回答by Jason Krs

You can use the following code :

您可以使用以下代码:

<!DOCTYPE html>
<html>
    <body onLoad="triggerJS();">

    <script>
        function triggerJS(){
        location.replace("http://www.google.com");
        /*
        location.assign("New_WebSite_Url");
        //Use assign() instead of replace if you want to have the first page in the history (i.e if you want the user to be able to navigate back when New_WebSite_Url is loaded)
        */
        }
    </script>

    </body>
</html>