How to go back to home page in javascript

To redirect a URL page with JavaScript you need to set the window.location object. There are several ways to change the location property on the window object:

  • window.location.href - returns the URL of the current page.



  
    Title of the document
  
  
    
      let url = "//www.w3docs.com";
      window.location.href = url;
    
  

  • window.location.hostname - returns the name of the Internet host of the current page.



  
    Title of the document
  
  
    Click and go to the page
    
      function redirectFunc[] {
        window.location.href = "//www.w3docs.com/";
      }
    
  

  • window.location.replace - removes current URL from the history and replaces it with a new URL which disables to go to the previous page using the back button.



  
    Title of the document
  
  
    
      function redirectFunc[] {
        window.location.replace["//www.w3docs.com/"];
      }
      setTimeout["redirectFunc[]", 2000];
    
  

  • window.location.assign - keeps the history and makes it possible to go back to the original page just clicking on the back button.



  
    Title of the document
  
  
    
      function redirectFunc[] {
        window.location.assign["//www.w3docs.com/"];
      }
      setTimeout["redirectFunc[]", 2000];
    
  

You had better use window.location.replace instead of .href because the replace method navigates to the URL without adding a new record to the history; thus the reader won’t be lost in an endless back-button mess.

Use .href if you want to make the user click on a link, and use .replace if you're going to make an HTTP redirect.

If JavaScript is disabled in the browser, this will not work.

Learn how to redirect web pages with HTML, PHP , Apache and Node.js .

Back to previous page As you can see this requires JavaScript to be enabled in the client browser. Otherwise this code will not work.

  • Submit form data to new window
  • Using buttons to link different pages

This article is written by plus2net.com team.


plus2net.com

How do I go back to previous page in JavaScript?

The history. back[] method loads the previous URL [page] in the history list. The history. back[] method only works if a previous page exists.

How do I go back to my homepage in HTML?

You can use the history. back[] method to tell the browser to go back to the user's previous page.

How do I go back to previous page?

Move between screens, webpages & apps Gesture navigation: Swipe from the left or right edge of the screen. 2-button navigation: Tap Back . 3-button navigation: Tap Back .

What is history back []?

The History. back[] method causes the browser to move back one page in the session history. It has the same effect as calling history.go[-1] . If there is no previous page, this method call does nothing. This method is asynchronous.

Bài mới nhất

Chủ Đề