简体   繁体   English

如果url相同,则JS重定向/窗口替换将不起作用(但添加了锚点)

[英]JS redirect/window replace doesn't work if url is the same (but anchor added)

I want to use JS/jQuery to reload the current page and jump to an anchor. 我想使用JS / jQuery重新加载当前页面并跳转到锚点。

I've tried using: 我尝试过使用:

window.replace('currentpage/#msg'+.message_id);

And

window.location.href ='currentpage/#msg'+.message_id;

Both change the url in the address but fail to reload the page. 两者都更改了地址中的URL,但无法重新加载页面。

Any other ways to do it? 还有其他方法吗?

call this after changing url : 更改网址后调用:

window.location.reload(true)

There are more ways to do it: http://www.phpied.com/files/location-location/location-location.html :) 有更多方法可以做到: http ://www.phpied.com/files/location-location/location-location.html :)

Another way to force the redirect is to make sure that the URL does change, by adding in a random element. 强制重定向的另一种方法是通过添加随机元素来确保URL确实发生了更改。 I often add a timestamp to the end of a URL. 我经常在URL末尾添加时间戳。

This has the added benefit or drawback that each page load will look unique to caching systems/etc. 这具有额外的优点或缺点,即每个页面加载对于缓存系统/ etc而言都是唯一的。 You may or may not want this. 您可能会或可能不会想要这个。

Try this way 试试这种方式

First get the current URL 首先获取当前网址

var CurUrl = window.location;

Now change the redirect URL in the browser without further' 现在,无需进一步更改浏览器中的重定向URL。

history.pushState(CurUrl, "OldPage", "NewPage");

Now the browser URL has changed but the page was not redirected and get the URL again' 现在,浏览器URL已更改,但页面未重定向,然后再次获取URL'。

var UrlRedirect = window.location;

here you can write your code 在这里你可以写你的代码

Finish redirecting 完成重定向

window.location = UrlRedirect;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM