如何用JS(在Rails中)不引人注意地更新页面标题

Tom*_*man 3 javascript ruby-on-rails dry unobtrusive-javascript

每当我使用Ajax将博客帖子加载到页面上时,我将页面设置<title>为"我的博客 - BLOGPOST_TITLE".

当然,"我的博客 - "也出现在我的应用程序布局中.

问题是,我怎么告诉我的Javascript字符串"我的博客 - "而不是在我的代码中重复它?

Svi*_*huk 7

在将Ajax发送到服务器之前,将document.title值("我的博客")发送到某个变量.然后当响应到达时,将document.title设置为document.title +' - '+ BLOGPOST_TITLE

所以你有HTML:

... <title>我的博客</ title> ...

在JS中:

var TITLE = document.title;

function getBlogSpotEntry() {
   Ajax.Request(url, {
     onSuccess: function(response) {
       var entryTitle = getTitle(response.responseText);

       document.title = TITLE + " - " + entryTitle;
     }
   })
}