简体   繁体   English

$(window).load Chrome,Safari 问题

[英]$(window).load Chrome,Safari problem

Ok here is the problem好的,这是问题所在

$(window).load(function () {
        \\Do something
    });

and other variations of this just don't work in chrom and safri.和其他变体在 chrom 和 safri 中不起作用。 FF,IE opera work fine I search but didn't find any working solution someone know how to check in chrome,safari when the page has finished loading? FF,IE Opera 工作正常我搜索但没有找到任何工作解决方案有人知道如何在页面加载完成后检查 chrome、safari?

You can try one of the below code if you want to execute something on page load.如果您想在页面加载时执行某些操作,您可以尝试以下代码之一。

$(document).ready(function(){
    //Page loaded
});

OR要么

$(function(){
    //Page loaded
});

EDIT: Try this编辑:试试这个

window.onload = function(){
   //Page loaded
};

Or if you want to use jQuery then try this或者如果你想使用 jQuery 然后试试这个

$(window).bind('load', function(){
   //Page loaded
});

I have a page that loads fine in FF but doesn't work in Chrome because I have 2 scripts using $(window).load(function() and the 2nd seems to execute before the images appear.我有一个页面可以在 FF 中正常加载但在 Chrome 中不起作用,因为我有 2 个使用 $(window).load(function() 的脚本,而第二个脚本似乎在图像出现之前执行。

The first script checks for missing images and substitutes an error image:第一个脚本检查丢失的图像并替换错误图像:

   $(window).load(function() {
    $("img").each(function(){
      var image = $(this);            
      if(image.context.naturalWidth == 0 ||
      image.readyState == 'uninitialized'){   
         $(image).unbind("error").attr(
             "src", "assets/img/image_missing.jpg"
         );
      }
 });
});  

The second function realigns elements on a row, in case the window is resized (it's a responsive page):第二个函数重新排列一行中的元素,以防窗口大小调整(这是一个响应式页面):

$(window).load(function() {
        columnConform();
});

When I use debug on this in Chrome, the first function is executed, but when the 2nd is executed any images that are missing have not been displayed.当我在 Chrome 中对此使用 debug 时,会执行第一个函数,但是当执行第二个函数时,任何丢失的图像都没有显示。 They only appear after the 2nd function has completed.它们仅在第二个功能完成后出现。

When the page first loads, the image for "Socketry" is not found, so Spanners/Metric/Imp is displayed immediately underneath on the same row.页面首次加载时,未找到“Socketry”的图像,因此 Spanners/Metric/Imp 显示在同一行的正下方。

The 2nd script re-aligns the rows assuming this to be correct, however when "Image Missing" is then displayed, this forces the Spanners onto the next row.假设这是正确的,第二个脚本重新对齐行,但是当显示“图像丢失”时,这会强制扳手到下一行。

"Image Missing" should be displayed prior to the row alignment, in which case the rows will align correctly, as they do in Firefox. “图像缺失”应该在行对齐之前显示,在这种情况下,行将正确对齐,就像在 Firefox 中一样。

* I would have posted screen shots but I don't have 10 reputation - ggrrrhh! *我会发布屏幕截图,但我没有 10 名声望 - ggrrrhh! Sometimes a picture is worth 1,000 words... *有时一张图片值 1,000 字... *

Guess I had better desctibe the page in words...猜猜我最好用文字描述页面...

I have 13 images displayed 6 to a row, so 2 full rows and 1 on a 3rd row.我有 13 张图像显示为 6 行,所以 2 个完整的行和 1 个在第三行。 The 6th image cannot be found, so the 7th displays right under the missing image symbol on the top row, instead of as the 1st image on the 2nd row.找不到第 6 个图像,因此第 7 个图像显示在顶行缺失图像符号的正下方,而不是作为第 2 行的第 1 个图像。 The 2nd script aligns the rows, assuming that the 7th image is on the top row, and then the 1st script makes the "Image Missing" image appear, which forces the 7th image to the 2nd row, on it's own.第二个脚本对齐行,假设第 7 个图像在顶行,然后第 1 个脚本使“图像丢失”图像出现,这迫使第 7 个图像自行排在第 2 行。 The 2nd script needs to execute after all images are loaded.加载所有图像后需要执行第二个脚本。 Hope that makes sense.希望这是有道理的。

window.loaddocument.ready都适合我,这是一个小提琴

I don't think this will answer your question, but in my case load() wasn't working in Chrome alone because I was trying to load to a <form> that was inside another <form> .我不认为这会回答你的问题,但在我的情况下load()并没有在Chrome中单独工作,因为我试图加载到<form>这是另一个里面<form> Chrome completely ignores the existence of the inner <form> . Chrome 完全忽略了内部<form>的存在。

Try downloading an uncompressed version of jQuery and debugging it.尝试下载未压缩版本的 jQuery 并对其进行调试。

This can happen in some browsers (I've experienced it in Opera and Chrome) when working on localhost.在本地主机上工作时,这可能会发生在某些浏览器中(我在 Opera 和 Chrome 中经历过)。 The "ready" event seems to fire before the javascript has a chance to add the listener so it $(window).ready never fires. "ready" 事件似乎在 javascript 有机会添加侦听器之前触发,因此 $(window).ready 永远不会触发。

Example: Run a function when the page is fully loaded including graphics.示例:当页面完全加载(包括图形)时运行函数。

  $( window ).load(function() {
         // Run code
         });

Example: Add the class bigImg to all images with height greater than 100 upon each image load.示例:在每次加载图像时将 bigImg 类添加到高度大于 100 的所有图像。

$( "img.userIcon" ).load(function() {
   if ( $( this ).height() > 100) {
     $( this ).addClass( "bigImg" );
   }
});

For more information, see http://api.jquery.com/load-event/ .有关更多信息,请参阅http://api.jquery.com/load-event/

Try this way:试试这个方法:

$(document).ready(function(){
   //Do something
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM