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

javascript - Possible to use slash () instead of hash (#) without page refresh window.location? - Stack Overflow

programmeradmin4浏览0评论

I use 'window.location.hash' to add '#something' to the URL without refreshing the page.

I want to know how to do the same but using a slash (/) instead of hash (#).

Why? I have navigation tabs and I use a jQuery and Ajax to dynamically load the data. When javascript is enabled, '#something' is added to the end of the URL to get the data. When javascript is disabled, it redirects to '/something'. So I want to fake the same URL for both.

Instead of ->

Thanks.

I use 'window.location.hash' to add '#something' to the URL without refreshing the page.

I want to know how to do the same but using a slash (/) instead of hash (#).

Why? I have navigation tabs and I use a jQuery and Ajax to dynamically load the data. When javascript is enabled, '#something' is added to the end of the URL to get the data. When javascript is disabled, it redirects to '/something'. So I want to fake the same URL for both.

Instead of http://site./section#something -> http://site./section/something

Thanks.

Share Improve this question asked Jun 23, 2010 at 9:47 jonagoldmanjonagoldman 8,75421 gold badges74 silver badges103 bronze badges 2
  • 2 Why don'y you try http://site./section#/something/anotherthing – Mithun Sreedharan Commented Jun 23, 2010 at 9:50
  • 2 No. See stackoverflow./questions/352343/… – Anurag Commented Jun 23, 2010 at 10:08
Add a ment  | 

4 Answers 4

Reset to default 3

You can use any bination after the hash you want, but the answer to your question is no, you can't do what you're asking without re-directing the user.

Being able to play with the URL without re-directing would be a security concern on some levels (can you change the domain too? why not?....see where this rabbit hole goes?). For example changing your URL via JavaScript to say: http://www.mybank. (why isn't my bank using SSL? bad bank, bad!) would be a phishers dream...so browsers don't allow messing with the URL like this at all...not without actually taking you there.

Take a look at this article. Basically, it lets you do this:

history.pushState({}, 'New Title', 'new_page.html');

This updates the history and the location bar but not actually load the page. That's what you want, but it's part of HTML5, and few (if any) browsers support it at the moment. Sticking with hashes is a better idea.

History.pushState (see the link in @Casey's post, or Kyle Scholz' blog) is present in the latest versions of Safari and Firefox, and Modernizr 1.5 now tests browser support for it. I just starting playing around with this today and it appears to do exactly what you want.

I realize this doesn't help with older browsers; some kind of window.location.hash trick will still be needed there.

Why don't you deploy SWFAddress?

It does get you URLs in the form of ../#/section/something and should be pretty much what you need. It's widely used for many Flash/AJAX websites on SEO considerations.

发布评论

评论列表(0)

  1. 暂无评论