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>
This question is related to
javascript
jquery
ruby-on-rails
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
});
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.
The correct way to do this is on the server side.
In your layout, there at some point will be some code which puts the text in the div. Make this code also set some instance variable such as @page_title
, and then in your outer layout have it do <%= @page_title || 'Default Title' %>
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
});
document.title was not working for me.
Here is another way to do it using JQuery
$('html head').find('title').text("My New Page Title");
<script type="text/javascript">
$(document).ready(function() {
$(this).attr("title", "sometitle");
});
</script>
document.title was not working for me.
Here is another way to do it using JQuery
$('html head').find('title').text("My New Page Title");
<script type="text/javascript">
$(document).ready(function() {
$(this).attr("title", "sometitle");
});
</script>
This works fine in all browser...
$(document).attr("title", "New Title");
Works in IE too
Do not use $('title').text('hi')
, because IE doesn't support it.
It is better to use document.title = 'new title';
If you have got a serverside script get_title.php that echoes the current title session this works fine in jQuery:
$.get('get_title.php',function(*respons*){
title=*respons* + 'whatever you want'
$(document).attr('title',title)
})
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.
The correct way to do this is on the server side.
In your layout, there at some point will be some code which puts the text in the div. Make this code also set some instance variable such as @page_title
, and then in your outer layout have it do <%= @page_title || 'Default Title' %>
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>
Do not use $('title').text('hi')
, because IE doesn't support it.
It is better to use document.title = 'new title';
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>
If you have got a serverside script get_title.php that echoes the current title session this works fine in jQuery:
$.get('get_title.php',function(*respons*){
title=*respons* + 'whatever you want'
$(document).attr('title',title)
})
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.
The correct way to do this is on the server side.
In your layout, there at some point will be some code which puts the text in the div. Make this code also set some instance variable such as @page_title
, and then in your outer layout have it do <%= @page_title || 'Default Title' %>
Source: Stackoverflow.com