能否从 HTML 页面加载 JavaScript 程序,然后让 JavaScript 加载另一个 HTML 页面而不是加载程序的页面?
能否从 HTML 页面加载 JavaScript 程序,然后让 JavaScript 加载另一个 HTML 页面而不是加载程序的页面?
可以。在 JavaScript 代码中:
window.location.href = "http://new.website.com/that/you/want_to_go_to.html";
标签样式的脚本。
window.location.href = url;
url是您希望加载的网址。
是否有可能(仅在线工作并仅加载您的页面或文件):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代替qr.onload。
<!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>
window.location.replace()
或 window.location.assign()
或 window.location.href
或 window.open()
这些方法之外,还有一种方法是触发表单提交:var form = document.createElement("form");
form.method = "POST";
form.action = "//test.local/login.php";
form.target = "_blank";
form.style.display = 'none';
var input1 = document.createElement("input");
input1.value = "123";
input1.name = "param1";
form.appendChild(input1);
var input2 = document.createElement("input");
input2.value = "abc";
input2.name = "param2";
form.appendChild(input2);
document.body.appendChild(form);
form.submit();
method
属性改为GET
:https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/form#method
<script>
标签引用JS文件;3)该JS文件更改浏览器的位置到另一个页面或者 3)该JS文件加载部分HTML页面并将其用于更新当前页面的某个部分。 - Doug Neiner