All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Redirect On Page Load

Last Updated : Mar 11, 2024

JavaScript Redirect On Page Load

In this tutorial we will show you the solution of JavaScript redirect on page load, here we need to use ‘window.onload’ then only we can redirect to some page when we loads our program.

The onload event occurent when an object has been loaded.

Onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, css files,etc.).

Step By Step Guide On JavaScript Redirect On Page Load :-

Here we defined script with ‘onLoad’ event for load javascript function so when user loads program on browser it loads function what we defined.

Within that function we defined ‘window.location.replace’ it contains path information of file need to navigate.

The location object contains information about the current url, it is property of the window object.

The ‘replace’ used to replace the current url in the browser address bar for move to that file page.

<!DOCTYPE html>
<html>
    <head>
        <title>Window Onload</title>
    </head>
    <body>
        <script>
            window.onload=function(){
                window.location.replace="www.google.com";
            }
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Within <script> tag we defined our javascript program, we defined onLoad event with window service so it will loads anything what we defined within that.
  7. Here we defined function for navigate to provided ‘www.google.com’ url information of particular webpage by ‘window.location.replace’ service.
  8. The replace service will replacing url to google site url when we executes program on browser and also we can use another services ‘window.location.href, window.location.assign’ in javascript result will same.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion we are able to know how to use on page load to redirect in JavaScript.

When we executes our program on browser, the current page location on address bar will replaced to ‘www.google.com’ defined in function so it will navigate to google webpage.

If your navigate to any other websites you need to do only one thing is change the url not necessary to give same as url above we given.

We can also use another two more methods in javascript we seen above.

Later we will see about them in detail. I hope this tutorial on JavaScript redirect on page load helps you and the steps and method mentioned above are easy to follow and implement.