开发者

JS to update the window Location with a URL not a HASH

I'm building a web app for Sign In / Registration etc, and do not want the page to have to refresh.

www.mysite.com

I know how to set a hash on the URL without refreshing the page:

location.hash

to give: www.mysite.com/#signin

But what I want: www.mysite.com/signin

Without the hash so it's SEO friendly. Also, once I set that. How in JavaScript can I read the first path after the / so I can detect 开发者_如何学GoSignIn, Register,AboutUs etc, and show the appropriate content?

Thanks!


document.location = "signin";

This will refresh the page. There's no way around it without using an anchor (hash).

Then to read the page you're on:

var page = document.location;
var path = page.pathname;

if (path == "signin") {
    // do something
}


You can't change the location via JS without forcing a page-load. Only the hash part can be changed without forcing a page load.


Assuming your "signin" content is truly on the same page then the hash has no advantage or disadvantage as the page will be crawled and indexed regardless of the URL format. See this SO Post.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜