Do you know you can earn heaps of $$ by answering questions on kodlogs, for more info contact us through feedback link at footer? We are Australian based web site and pay you for answering questions

  • Register
Welcome to Kodlogs Q&A, where you can ask programming questions and receive answers from other members of the community. For programmers, by programmers Kodlogs is an open community for anyone that does coding. We help you get answers to your toughest coding questions, share knowledge with your coworkers in private, and find your next IT dream job.
0 votes
10 views
How do you redirect a page to another page in JavaScript on page load?  I am required to redirect to another page when page loads , how do i do this? I want to see full code if anyone can provide?
by (930 points)  

2 Answers

0 votes

Redirect to another page on load using JavaScript

Javascript redirect on page load

You can use server-side redirects, HTML meta refresh redirects and JavaScript redirects to redirect a page to another page in Javascript.

Lets see this through some examples to redirect a page using Javascript.

User does not appreciate experience if their are interruptive or unexpected web page redirects, as this will cause frustration and annoyance to users.

For instance, in the event that you divert the clients to another site when they arrive on your site, that will clearly cause disappointment for them. Additionally, in the event that you take the clients to an unessential page after a clock, after they click on a catch or picture, or after they complete a specific activity on your site, that will most likely bring about the client needing to leave your site on the double and never return.

Another reason you ought to keep away from redirect where conceivable is that the web search tools don't support sites that utilization diverts, particularly in the event that they are misleading. Your webpage may even get expelled from their file on the off chance that you have diverts on your site that attempt to deceive the web index bots or that outcome in an awful client experience. Having said that, there are additionally a few situations where a divert can be very helpful dependent on the setting of your site page or a particular element of your web application and we will leave the choice whether to utilize a divert or not, to you.

Lets now see how JavaScript redirects work.

JavaScript Redirect Methods

Some of redirect useful methods:

// used for new location of the current window.

window.location = "https://www.kodlogs.com";

// used for the new href (URL) for the current window.

window.location.href =  "https://www.kodlogs.com";

// used for a new URL to the current window.

window.location.assign("https://www.kodlogs.com";);

//used for replacing the location of the current window with the new one.

window.location.replace("https://www.kodlogs.com";);

// used for the location of the current window itself.

self.location =  "https://www.kodlogs.com";;

// used for the location of the topmost window of the current window.

top.location =  "https://www.kodlogs.com";;

JavaScript Redirect: Redirect to another Page on Page Load

<script>

  window.location.href = "https://www.kodlogs.com";

</script>

JavaScript Redirect: Redirect the another Page After a some time or Period

<script>

setTimeout(function() {

  window.location.href = "https://www.kodlogs.com";

}, 9000);

</script>

JavaScript Redirect: Redirect the Page After an Event or User Action

<script>

// Check if the condition is true and then redirect.

if ( ... ) {

  window.location.href = "https://www.kodlogs.com";

}

</script>

// onclick event is assigned to the #button element.

document.getElementById("button").onclick = function() {

  window.location.href = "https://www.kodlogs.com";

};

</script>

Conclusion:

So we have seen in illustration above how JavaScript redirect is used . The above code could be helpful for  you while handling your web page redirects.

by (930 points)  
edited by
0 votes

Some javascript methods to do page redirect to another page on load:

Redirect Javascript part:

window.onload = function() {

    window.location.href = "http://somedomain.com";

}

window.onload = function() {

 window.location.replace("http://somedomain.com");

}

HTML Part:

<html>

  <head>

    <meta http-equiv="refresh" content="0;url=http://somedomain.com" />

    <title></title>

  </head>

 <body></body>

</html>

Another way is to use onload event in body tag of HTML

<html>

  <head>

    <meta http-equiv="refresh" content="0;url=http://somedomain.com" />

    <title></title>

  </head>

 <body></body>

</html>

Yet another way is :

<body onload="window.open(somepage)">

Another way is:

<script type="text/javascript">

    window.location = "somepage.html";

</script>

by (930 points)  
...