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

javascript - Redirect to another page with button or anchor in jQuery - Stack Overflow

programmeradmin1浏览0评论

I have a different scenario, I want to redirect the page on click event to a link/page which contains # sign inside it. when I click on it, page remain on the same location, doesn't redirect, just replace the url in url box. I guess this is because # sign, when I replace with another link without # sign it works fine. I cannot remove this from page link, How do I redirect my page? I want to perform this in jQuery

edit: I don't want to reload my page

edit: not duplicate my link is separated by slashes "/" after and before # sign, not attached with page name

link where I want to redirect

myApp/#/myPage.html

or just

#/myPage.html

HTML

<button id="btn">Click</button>

or

<a id="btn" href="#">Click</a>

I tried these..

<button onClick="window.location='#/myPage.html';">Click</button>

and

$(document).on('click', '#btn', function (e) {
                    window.location.href = "myApp/#/myPage.html";  
//window.location.href = "#/myPage.html";  // this too tried
             });

and these

window.location.href = "#/myPage.html";
$(location).attr('href','#/myPage.html');

I have a different scenario, I want to redirect the page on click event to a link/page which contains # sign inside it. when I click on it, page remain on the same location, doesn't redirect, just replace the url in url box. I guess this is because # sign, when I replace with another link without # sign it works fine. I cannot remove this from page link, How do I redirect my page? I want to perform this in jQuery

edit: I don't want to reload my page

edit: not duplicate my link is separated by slashes "/" after and before # sign, not attached with page name

link where I want to redirect

myApp/#/myPage.html

or just

#/myPage.html

HTML

<button id="btn">Click</button>

or

<a id="btn" href="#">Click</a>

I tried these..

<button onClick="window.location='#/myPage.html';">Click</button>

and

$(document).on('click', '#btn', function (e) {
                    window.location.href = "myApp/#/myPage.html";  
//window.location.href = "#/myPage.html";  // this too tried
             });

and these

window.location.href = "#/myPage.html";
$(location).attr('href','#/myPage.html');
Share Improve this question edited Aug 24, 2016 at 8:15 Mangrio asked Aug 24, 2016 at 7:50 MangrioMangrio 1,0301 gold badge21 silver badges46 bronze badges 6
  • Possible duplicate of How to link to a <div> on another page? – online Thomas Commented Aug 24, 2016 at 8:04
  • @Thomas I have a slash "/" after and before # sign – Mangrio Commented Aug 24, 2016 at 8:06
  • Mnagrio stackoverflow./questions/70579/… No slashes allowed – online Thomas Commented Aug 24, 2016 at 8:14
  • @Thomas $(location).attr('href','myApp/directory/myPage.html'); this works fine with slashes – Mangrio Commented Aug 24, 2016 at 8:17
  • If you are using URLs such as myApp/#/myPage.html, then that surely is some kind of framework with a built-in routing system? As such, it should provide methods to go to a “different page”, which effectively seems to be what you want to do. So go check the documentation of that framework. – C3roe Commented Aug 24, 2016 at 8:45
 |  Show 1 more ment

2 Answers 2

Reset to default 3

As long as the URL stays the same, the browser won't make a new page request. You could add some query parameter to the URL like this, the downside being slightly less pretty URL in the location bar:

$(document).on('click', '#btn', function (e) {
     window.location.href = "?"+Date.now()+"#/myPage.html";
});

I think this stackoverflow reply answers your question: Javascript reload the page with hash value

window.location.hash = "#/myPage.html" location.reload();

发布评论

评论列表(0)

  1. 暂无评论