Pranav
Pranav

Reputation: 3312

Loading another html page from javascript

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?

Upvotes: 24

Views: 142912

Answers (5)

gavenkoa
gavenkoa

Reputation: 48923

Another approach, besides window.location.replace() or window.location.assign() or window.location.href or window.open(), is to trigger form submission:

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();

You could change the form method to GET: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#method

Upvotes: 0

Alexander V. Ulyanov
Alexander V. Ulyanov

Reputation: 386

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

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.

Upvotes: 9

Jason Krs
Jason Krs

Reputation: 714

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>

Upvotes: 0

Ravi Vanapalli
Ravi Vanapalli

Reputation: 9950

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

window.location.href = url;

Where url would be the url you wish to load.

Upvotes: 11

Grant Paul
Grant Paul

Reputation: 5902

Yes. In the javascript code:

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

Upvotes: 35

Related Questions