AppDividend
Latest Code Tutorials

How To Redirect To Another Web Page Using Javascript

342

Get real time updates directly on you device, subscribe now.

How To Redirect To Another Web Page Using Javascript is today’s leading topic.  Here we need to use Clientside javascript or browser-based Javascript because we can not use the window object on the node.js server. It is defined in the browser. So for this example, we will not use Node.js.

JavaScript offers many ways to redirect the user from one webpage to a different web page. We can do it programmatically.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Redirect To Another Web Page Using Javascript

The following one type can be considered as canonical to navigate to a new URL is

window.location = 'https://appdividend.com';

If you want to redirect to a different path, on the same domain, use:

window.location.pathname = '/yournewpath';

#History API

The DOM window object provides access to the browser’s history through the history object. It exposes the useful methods and properties that let you move back and forth through the user’s history. If you have used any frontend frameworks like Angular, React, or Vue then you might be familiar with history API. In this structure of a History API, the hash(#) is not there.

Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Join the program

#Other Options To Redirect

In this browser, we can access the global object called window.  So you can do the following.

location = 'https://appdividend.com';

There is also another way is to set the href property of location.

window.location.href = 'https://appdividend.com';

The location() also has an assign() method that accepts a URL and performs the same thing.

window.location.assign('https://appdividend.com');

The browser exposes the self and top objects, which all reference the window object, so you can use them instead of the window in all the examples above.

self.location = 'https://appdividend.com';

top.location = 'https://appdividend.com';

#Location replace() Method

The replace() method replaces the current document with a new one document.

The significant difference between this method and assign(), is that the replace() removes the URL of the current document from the document history, meaning that it is not possible to use the “back” button to navigate back to the original document. It completely replaces it.

Finally, How To Redirect To Another Web Page Using Javascript tutorial is over.

 

2 Comments
  1. michael petre says

    Appreciating the dedication you put into your site and detailed information you provide.

    1. Krunal says

      Thanks man!!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.