How can I change the title of the document during .ready()?
I am using some nested layouts in Ruby on Rails, and in one of the layouts i have a need to read in a string from a div and set that as the title of the document. What is correct way (if any) to set the title of the document?
<script type="text/javascript">
$(document).ready(function() {
// ???
});
</script>
The following should work but it wouldn't be SEO compatible. It's best to put the title in the title tag.
<script type="text/javascript">
$(document).ready(function() {
document.title = 'blah';
});
</script>
Do not use $('title').text('hi')
, because IE doesn't support it.
It is better to use document.title = 'new title';
This works fine in all browser...
$(document).attr("title", "New Title");
Works in IE too
Like this:
$(document).ready(function ()
{
document.title = "Hello World!";
});
Be sure to set a default-title if you want your site to be properly indexed by search-engines.
A little tip:
$(function ()
{
// this is a shorthand for the whole document-ready thing
// In my opinion, it's more readable
});
<script type="text/javascript">
$(document).ready(function() {
$(this).attr("title", "sometitle");
});
</script>