Rails ujs let us use other javascript frameworks (like jQuery) in our project. That’s cool.
Here is an article about top 10 javascript frameworks by google. He use a way (an easy way) to sort these frameworks. Search “javascript frameworks” in Google. You can see that the default rails javascript frameworks prototype is not the most popular one now.
I’m working on a small project for my company by rails and jQuery. The newest rails3 use many html5 features. So I decide let user use a modern browser to use my web page. Otherwise IE6 will kill me. How to switch smoothly? I found chrome frame has beta yesterday!
Someone said that they hate the web page of xxx-browser only. But wait! I use this in my company only! Didn’t you saw those enterprise-level soft let you install their plugin?
Make an IE-only page is easy. Make an IE-except page is easier. lol
Chrome frame show my html5 page (maybe). It’s beautiful than IE. But it cannot works when I send ajax request.
For example I write a link like this:
1 | <!-- Rails html5 templete --> |
But chrome frame look like send a incorrect request to rails. Check the log of rails, I found this line:
Processing by UsersController#new as */*
The right process method is JS because the data type is script. I have test IE FF Chrome and Safari on win and mac. Only chrome frame has this issus.
I think I should fix this at client side. When I try to get navigator.userAgent by javascript. I found it has the same value as chrome stand-alone.
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.62 Safari/533.4
But rails could detect the right user agent:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) chromeframe/5.0.375.62
Oh! Javascript don’t know it’s running in chrome frame, so it can do anything. Rails know it’s processing the request from chrome frame, but it don’t know that’s a ajax request.
After a minute. I decide let rails tell javascript: “u r working for chrome frame.”
1 | <!-- Hold it in application.html.erb or ur own default layout. for this example put in navigator. --> |
1 | # Detect chrome frame in application_controller.rb |
And then hack $.ajax. In fact I have three things need fix.
- Chrome frame cannot send PUT DELETE verb.
- Append .js to url tell rails I’m a JS request.
- jQuery will try parse the result as JSON when dataType is script. If it got html. You will see an error: “Uncaught SyntaxError: Unexpected token <”.
1 | //BUGFIX: Chromeframe miss some features. |