如何使用JavaScript重定向到当前页面?

时间:2022-09-18 07:28:35

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

单击链接时,它应重定向到当前页面。我如何使用JavaScript执行此操作?

3 个解决方案

#1


37  

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

假设您的意思是链接应该刷新当前页面,您可以使用window.location.reload()。在jQuery中它看起来像这样:

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

In plain JS it would look like this:

在普通JS中,它看起来像这样:

document.querySelector("#myLink").addEventListener('click', function() {
    window.location.reload();
});

#2


3  

Here is a way to do it using vanilla JS without inlining it and without jQuery:

这是一种使用vanilla JS而无需内联并且没有jQuery的方法:

<a href="#" id="myLink">Refresh current page</a>  

<script>
    document.querySelector("a#myLink").onclick = function(){
        window.location.reload();
    }; 
</script>

note the list of supported browsers when considering using querySelector()

在考虑使用querySelector()时请注意支持的浏览器列表

#3


1  

Example using a button:

使用按钮的示例:

<input type="button" value="Reload Page" onClick="window.location.reload()">

See here:

http://www.mediacollege.com/internet/javascript/page/reload.html

#1


37  

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

假设您的意思是链接应该刷新当前页面,您可以使用window.location.reload()。在jQuery中它看起来像这样:

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

In plain JS it would look like this:

在普通JS中,它看起来像这样:

document.querySelector("#myLink").addEventListener('click', function() {
    window.location.reload();
});

#2


3  

Here is a way to do it using vanilla JS without inlining it and without jQuery:

这是一种使用vanilla JS而无需内联并且没有jQuery的方法:

<a href="#" id="myLink">Refresh current page</a>  

<script>
    document.querySelector("a#myLink").onclick = function(){
        window.location.reload();
    }; 
</script>

note the list of supported browsers when considering using querySelector()

在考虑使用querySelector()时请注意支持的浏览器列表

#3


1  

Example using a button:

使用按钮的示例:

<input type="button" value="Reload Page" onClick="window.location.reload()">

See here:

http://www.mediacollege.com/internet/javascript/page/reload.html