Thursday, July 4, 2024
HomeLanguagesJavascriptHow can a page be forced to load another page in JavaScript...

How can a page be forced to load another page in JavaScript ?

In this article, we will see how can a page be forced to load another page in JavaScript.

Approach: We can use window.location property inside the script tag to forcefully load another page in Javascript. It is a reference to a Location object that is it represents the current location of the document. We can change the URL of a window by accessing it.

Syntax:

<script>
    window.location = <Path / URL>
</script>

Example:

<script>
    window.location = "https://www.neveropen.co.za/"
</script>

So in the above example, we see that by changing the window.location Object inside Javascript we can change the URL of our window and thus successfully load any page forcibly from our Javascript without any href tag. We will build a small working sample to learn it practically. 

Below is the step by step implementation:

Step 1: Create a file named index.html. Add a heading and two buttons to it. One button forcefully loads a page with a live URL and the other button loads a local HTML page. In the <script> tag we have two functions, one loads gfg home page, and the second loads a local HTML page using window.location property.

index.html




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
        content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
</head>
  
<body>
    <h3>This is the original page</h3>
    <br>
  
    <button onclick="force_load_gfg()">
        Force Load GFG Page
    </button>
    <br><br>
  
    <button onclick="force_load_local()"
        Force Load Local HTML page
    </button>
  
    <script>
        function force_load_gfg() {
            window.location = 
                "https://www.neveropen.co.za/"
        }
  
        function force_load_local() {
            window.location = 
                "F:/gfg/PageRedirect/newPage.html"
        }
    </script>
</body>
  
</html>


Step 2: Create a file named newPage.html. This is the local HTML page that would be loaded by Javascript.

newPage.html




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
        content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title> New Page </title>
</head>
  
<body>
    <h3>This is the new loaded page</h3>
</body>
  
</html>


Output:

Output Screencast

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments