Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❀ đŸ’»
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

JavaScript - how to redirect to another web page?

0 contributions
8 points

In JavaScript there is available window.location object that helps to navigate to web page directly from font-end level (web browser). location object provides few properties and methods from which most useful for making redirection are:

  • location.replace method
  • location.assign method
  • location.href property
  • based on link element (a tag with href attribute)

For more details look at below examples.

1. location.replace method example

<!doctype html>
<html>
<body>
  <script>

    location.replace('https://dirask.com/about');

  </script>
</body>
</html>

Note: this redirection removes current web page from navigation history.

Result:

JavaScript - location.replace method redirection to Dirask About web page.
JavaScript - location.replace method redirection to Dirask About web page.

2. location.assign method example

<!doctype html>
<html>
<body>
  <script>

    location.assign('https://dirask.com/about');

  </script>
</body>
</html>

Note: this redirection keeps current web page in navigation history.

 Result:

JavaScript - location.assign method redirection to Dirask About web page.
JavaScript - location.assign method redirection to Dirask About web page.

3. location.href property example

<!doctype html>
<html>
<body>
  <script>

    location.href = 'https://dirask.com/about';

  </script>
</body>
</html>

Note: this redirection keeps current web page in navigation history.

Result:

JavaScript - location.href property redirection to Dirask About web page.
JavaScript - location.href property redirection to Dirask About web page.

4. Link element example

Note: run it in separated file to see effect

<html>
<body>
  <a id="link" href="https://dirask.com"></a>
  <script>
  
    var element = document.querySelector('#link');
    element.click();
  
  </script>
</body>
</html>

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more