简体   繁体   English

从javascript加载另一个html页面

[英]Loading another html page from javascript

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

Yes. 是。 In the javascript code: 在javascript代码中:

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

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。

Is it possible (work only online and load only your page or file): https://w3schools.com/xml/xml_http.asp Try 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也使用。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM