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

javascript - How to unobtrusively update the page title with JS (in Rails) - Stack Overflow

programmeradmin4浏览0评论

Whenever I load a blog post onto the page with Ajax, I set the page <title> to "My Blog - BLOGPOST_TITLE".

Of course "My Blog - " appears in my application layout as well.

The question is, how do I tell my Javascript about the string "My Blog - " without duplicating it in my code?

Whenever I load a blog post onto the page with Ajax, I set the page <title> to "My Blog - BLOGPOST_TITLE".

Of course "My Blog - " appears in my application layout as well.

The question is, how do I tell my Javascript about the string "My Blog - " without duplicating it in my code?

Share Improve this question asked Jun 2, 2009 at 6:27 Tom LehmanTom Lehman 89.4k72 gold badges205 silver badges279 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Before Ajax is sent to server store document.title value ("My Blog") to some variable. Then when response arrives set document.title to document.title + ' - ' + BLOGPOST_TITLE

so you have in HTML:

... < title>My Blog< /title> ...

and in JS:

var TITLE = document.title;

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

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

I would go this way (dirty, but works well):

document.myTitlePrefix = 'My Blog - '

and then update title as

document.title = document.myTitlePrefix + blogPostTitle
发布评论

评论列表(0)

  1. 暂无评论