开发者

$(window).load(function()-how it works with FF

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 w开发者_运维问答indow 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.


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.


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜