开发者

Full Ajax site, redirecting from "normal" URL to Ajax (fragment) URL automatically?

开发者 https://www.devze.com 2023-01-14 10:45 出处:网络
Ok, so all the rage these days is having a site like this: mysite.com/ mysite.com/about mysite.com/contact

Ok, so all the rage these days is having a site like this:

  • mysite.com/
  • mysite.com/about
  • mysite.com/contact

But then if the user has Javascript enabled, then to have them browse those pages with Ajax:

  • mysite.com/#/
  • mysite.com/#/about
  • mysite.com/#/contact

That's all well and good. I have that all working perfectly well.

My question is, if the user arrives at "mysite.com/about", I want to automatically redirect them to "mysite.com/#/about" immediately if they have Javascript.

I have it working so if they arrive at "mysite.com/about", that page will load fine on its own (no redirects) and then all clicks after that load via ajax, but the pre-fragment URL doens't change. e.g. if they arrive on "mysite.com/about" and then click "contact", the new URL will be "mysite.com/about#/contact". I really don't like that though, it's very ugly.

The only way I can think of to automatically redirect a user arriving at "mysite.com/about" to "mysite.com/#/about" is to have some javascript in the header that is ONLY run if the page is NOT being loaded via ajax. That code looks like this ($ = jQuery):

$(function(){ 
  if( !location.hash || location.hash.substr(1,1) != '/' ) {
    location.replace( location.protocol+'//'+location.hostname+'/#'+location.pathname+location.search ); 
  }
});

That technically works, but it causes some very strange behavior. For example, normally when you "view source" for a page that has some ajax content, that ajax content will not be in the source because you're viewing the original page's source. Well, when I view source after redirecting like this, then the code I see is ONLY the code that was loaded via Ajax - I've never seen anything like that before. This happens in both Firefox 3.6 and Chrome 6.0. I haven't verified it with other browsers yet but the fact that two browsers using completely different engines exhibit the same behavior indicates I am doing something bad (e.g. not just a bug with FF or Chrome).

So somehow the browser thinks the page I'm on "is" the Ajax page. I can continue to browse around and it works fin开发者_C百科e, but if I e.g. close Firefox and re-open it (and it re-opens the pages I was on), it only reloads the Ajax fragment of the page, and not the whole wrapper, until I do a manual refresh. (Chrome doesn't do this though, only Firefox). I've never seen anything like that.

I've tried using setTimeout so it does not do the redirect until after the page has fully loaded, but the same thing happens. Basically, as far as I can tell, this only works if the fragment is put there as the result of a user action (click), and not automatically.

So my question is - what's the best way to automatically redirect a Javascript capable browser from a "normal" URL to an Ajax URL? Anyone have experience doing this? I know there are sites that do this - e.g., http://rdio.com (a music site). No weirdness happens there, but I can't figure out how they're doing it.

Thanks for any advice.


This behavior is like the new twitter. If you type the URL:

http://twitter.com/dinizz

You will be redirected to:

http://twitter.com/#!/dinizz

I realize that this is done, not with javascript but in the server side. I am looking for a solution to implements this using ruby on rails. Although I suggest you to take a look on this article: Making AJAX Applications Crawlable

0

精彩评论

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