I've been looking around for answer to this but haven't found solution that works for my situation. I'm pulling in keywords dynamically from the URL and populating the page title with them. They are lower case and I need to find a way to make the first letter of each keyword Uppercase. This is the script I have so far:
var keywords = window.location.href.match(/kw=(.+)/)[1];
var array = keywords.split('+');
document.title = array.join("" + " ");
I thought this would be so simple but have struggled with it.
Any help would be greatly appreciated.
Thanks!
Update: this works
<script type="text/javascript">
var metas = document.getElementsByTagName('meta');
for (var i=0; i<metas.length; i++) {
if (metas[i].getAttribute("property") == "keywords") {
document.title = metas[i].getAttribute("content") + " ";
}
}
var mystr = document.title;
var subs = mystr.replace(",", " ");
var newtitle = toTitleCase(subs);
document.title = newtitle;
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
</script>