24

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?

3
  • 3
    What do you mean by "load". Do you mean this: 1) HTML page loads 2) References a JS file with a <script> tag 3) That JS file changes the location of the browser to another page OR 3) That JS file loads a partial HTML page and uses it to update part of the current page. Commented Jan 7, 2010 at 6:19
  • The first one. The javascript file loads using the onload parameter in the <body> tag. Then the javascript file displays an entirely different page. Commented Jan 7, 2010 at 6:45
  • I think I have answered it correctly. What you are saying is not possible because it is a violation of the same origin policy. Commented Feb 25, 2010 at 18:34

5 Answers 5

36

Yes. In the javascript code:

window.location.href = "http://new.website.com/that/you/want_to_go_to.html";
Sign up to request clarification or add additional context in comments.

4 Comments

but be aware, that this script isn't included/executed on the new page! otherwise you will get a recursive call
Would be better to use the functions (supported by all browsers) created for this purpose rather than using the assignment trick. location.assign and location.replace functions.
@Blaine what functions, can you point to the code?
@jkr As I said, location.assign and location.replace. They obviously take one string as parameter. If you can't figure out how to call a function given function name and parameter, I don't think you're cut out for coding. w3schools.com/jsref/met_loc_assign.asp and w3schools.com/jsref/met_loc_replace.asp.
11

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.

Comments

9

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.

1 Comment

This is the answer to question, not the href change that will redirect to another page. Instead it will get raw source code of the requested file as string.
0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.