最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How can I redirect a page to itself in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

Here is my current code which does redirecting:

window.location.href= '/post/' + data.main_id + '/' + subject + '#post-' +  data.post_id;

Code above just changes the slug when I'm in the same URL. anyway, I need to both change the slug and reload the page. How can I do that by JS or even jQuery?

Here is my current code which does redirecting:

window.location.href= '/post/' + data.main_id + '/' + subject + '#post-' +  data.post_id;

Code above just changes the slug when I'm in the same URL. anyway, I need to both change the slug and reload the page. How can I do that by JS or even jQuery?

Share Improve this question asked Dec 22, 2016 at 11:38 Martin AJMartin AJ 6,70712 gold badges71 silver badges137 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can achieve this by first appending the fragment to the URL, then reloading the page:

window.location.href += '#post-' + data.post_id;
window.location.reload();

Note however that you could improve this further by using pushState and avoiding the need to reload the page at all when the fragment changes.

You can use

location.reload();

Please, see http://www.w3schools./jsref/met_loc_reload.asp

发布评论

评论列表(0)

  1. 暂无评论