3

每当我使用 Ajax 将博客文章加载到页面上时,我都会将页面设置<title>为“我的博客 - BLOGPOST_TITLE”。

当然,“我的博客 -”也出现在我的应用程序布局中。

问题是,我如何告诉我的 Javascript 关于字符串“My Blog -”而不在我的代码中复制它?

4

2 回答 2

7

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

所以你有HTML:

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

在 JS 中:

var TITLE = 文档.title;

函数 getBlogSpotEntry() {
   Ajax.Request(url, {
     onSuccess:函数(响应){
       var entryTitle = getTitle(response.responseText);

       document.title = TITLE + " - " + entryTitle;
     }
   })
}
于 2009-06-02T06:32:24.910 回答
1

我会这样(很脏,但效果很好):

document.myTitlePrefix = 'My Blog - '

然后将标题更新为

document.title = document.myTitlePrefix + blogPostTitle
于 2009-06-02T06:32:43.240 回答