Reputation: 13641
I'm rubbish in javascript, but I need to do a little dynamic editing of my page title in the tab bar on my php site.
When the user on my site gets a mail, I want to change the title from "My site - Page" to "My Site (1) - Page"
I know how to change the page title with
<script>
window.parent.document.title = "My Site (x)";
</script>
But what I need to do is grab the current title, eg "My Site - Page", and change it to "My Site (x) - Page". So I would need some twiddling with the string.
Don't worry about what the x is, I just need a hand with the javascript not the PHP.
Upvotes: 0
Views: 290
Reputation: 66663
This should work (change X as needed):
window.parent.document.title = window.parent.document.title.replace('-', '(' + X + ') -');
Or you can simply do:
window.parent.document.title = 'My Site (' + X + ') - Page';
EDIT: Dynamic 'page' part:
function trimString(s) {
var l = 0, r = s.length - 1;
while(l < s.length && s[l] == ' ') l++;
while(r > l && s[r] == ' ') r -= 1;
return s.substring(l, r + 1);
}
var t = window.parent.document.title;
var page = trimString(t.split('-')[1]);
window.parent.document.title = 'My Site (' + X + ') - '+page;
Upvotes: 0
Reputation: 5042
var title = {
theme: "My Site (<@>)",
replace: function( txt ){
document.title = this.theme.replace('<@>', txt)
}
};
title.replace("something"); // title is My Site (something)
title.replace("something else"); // title is My Site (something else)
etc
Upvotes: 0
Reputation: 3074
here is how you can get and then set the title using javascript:
function showTitle(){
alert( document.title );
}
function changeTitle(txt){
document.title = txt;
alert( 'Title changed to ' + document.title );
}
In order to inject your php you would just pass it in as a parameter aka: the txt
parameter already in the function.
Upvotes: 0
Reputation: 66399
Something like this should work just fine:
var x = 5;
title = title.replace("My Site", "My Site (" + x + ")");
Upvotes: 1