jQuery: how to change title of document during .ready()?

Jason Miesionczek picture Jason Miesionczek · Oct 7, 2008 · Viewed 271.9k times · Source

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>

Answer

dpan picture dpan · Oct 7, 2008

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>