Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect to the current page using JavaScript?

On clicking a link, it should redirect to the current page. How do I do this with JavaScript?

like image 414
Surendar K Avatar asked Feb 21 '12 11:02

Surendar K


People also ask

Can we redirect a page using JavaScript?

In JavaScript, page redirection is simple. The window object's location object is a property. A web page can be redirected in a number of ways.

How do I redirect a specific part of a page using JavaScript?

You need to add ” id attribute” to the section you want to show and use the same id in href attribute with “#” in the anchor tag. So that On click a particular link, you will be redirected to the section that has same id mention in anchor tag.


1 Answers

Assuming you mean the link should refresh the current page, you can use window.location.reload(). In jQuery it would look like this:

<a href="#" id="myLink">Refresh current page</a> 
$("#myLink").click(function() {     window.location.reload(); }); 

In plain JS it would look like this:

document.querySelector("#myLink").addEventListener('click', function() {     window.location.reload(); }); 
like image 184
Rory McCrossan Avatar answered Oct 08 '22 21:10

Rory McCrossan