views:

61

answers:

3

Hi All, I have a jquery code.

$(window).load(function() {
    document.title = $("#myid").text(); //not working in FF
});

Here I have used $(window).load(function() because in the #myid I am getting value through another javascript, if I use ready(), its giving me error. so I am first loading the window then start reading value. Now in IE, after the window loads itself , I am getting the value of document.title, but for FF its coming as blank.undefined. Why? any idea or alternate sln.

+1  A: 

It might be a rendering/timing issue. How are you setting the #myid text? Im assuming you are running this code on page load? Personaly on another note, i like to use the shorthand version of jQuery DOM ready, this might also fix your problem too.

jQuery(function(){
   document.title = jQuery("#myid").text();
});

And i would make sure that you call it at the end of the body or ideally in the head tag.

danrichardson
if u use jQuery(function(){, u will not be able to get the value of jQuery("#myid").text(); this value is coming from another javascript, so if I call $(window).load(function() {, it will wait untill window load , then #myid will get populated, and then i will be able to retrive document.title = jQuery("#myid").text();
Wondering
I think you need to make your question a bit clearer. As what i said is on DOM finished. You are talking about something coming in through another resource. If it's coming through another javascript file, you should use the jQuery.getScript() function and then use a callback function to set the document title.
danrichardson
A: 

I think it is possible that firefox triggers ready and load at the same time when it loads quickly (localhost, small experiment page with one div, etc.)

Why not put the title setting in the ready function right after getting it? If You put it in a div, You can put it in the title too.

naugtur
A: 

I didn't check this code and it isn't a good way, but maybe it help you... If your code isn't working in Firefox only, you can check browser by Javascript and execute my code for Firefox only.

<script type="text/javascript">
var timerId = 0;

function checkElement() {
  // If don't work: try .html() or $("#myid").text() != undefined or smth like this
  if($("#myid").text()) {
    document.title = $("#myid").text();
    clearInterval(timerId);
  }
}

$(document).ready(function() {
  timerId = setInterval('checkElement()', 500);
});
</script>
Vladimir