document.ready & window.onload vs CSS Media Queries

Ahmet Umut Alkan

While doing research on CSS and JS codes that run before the user sees the content, something was stuck to my mind. When exactly were CSS media queries were running? Which js and jquery function run first (document.onload, document.ready etc.)? When I did research on it I found This and This questions which explain the difference between domcontentloaded, document.ready and window.onload functions. However, I could not figure out when exactly media queries in CSS run compared to these functions. So I checked it through JSFiddle in here.

In HTML part I only wrote a div with and ID;

<div id="testDiv">
  Test Media Queries And JS Functions
</div>

In CSS there was only 1 media query that ran;

@media screen and (min-width:1px){
  #testDiv{
    color: black;
  }
}

And in JS part, there was different load functions

$('document').ready(function() {
  document.getElementById("testDiv").style.color = "blue";
});

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("testDiv").style.color = "red";
});

window.onload = function() {
  document.getElementById("testDiv").style.color = "yellow";
};

The CSS code and its effects were visible for a brief moment. And than the div was turning to blue when I ran the code. However, I encountered a strange problem. Only in Google Chrome New Tab (Not Incognito), the code ran as yellow which means window.onload was running last and in all other browsers and tabs shows the div as blue.

Google Chrome Not Incognito Tab

Google Chrome Incognito Tab and Safari New Tab

First of all is this some kind of bug that I encountered or somehow is it intended to run like this?

Second of all, I could not find any documentation on when exactly media queries run compared to these functions.

If anyone has a link for documentation on CSS queries and their exact execution time, I would really appreciate it.

AamirSohailKmAs

Second Answer First

From Here You already familiar that if you use Javascript (that is not async or defer) then the DOM creation will wait for the JS to load. Since JS also modifies CSS, so JS will wait for the CSSOM to load, and in this way you got the answer that media queries runs earlier.

Now First one

Basically

The DOMContentLoaded event is fired when the DOM is created.

The jQuery $(document).ready() event is fired when the DOM is ready.

The load event is fired when the DOM, CSSOM and all other resources are loaded (jQuery library is one of them).

In this case

First DOM creation will wait for the JS to load when it is loaded then DOM is created, and DOMContentLoaded event fires at first place so color:red overrides color:black applied by media query.

Now load event is fired at second place because JS is already loaded and DOM and CSSOM is also loaded so color:yellow overrides color:red,

And document.ready event is fired at third place when DOM is ready so color:blue overrides color:yellow. and you see color blue.

Important

If you add an image just like Here then you'll see that

First DOM creation will wait for the JS to load when it is loaded then DOM is created, and DOMContentLoaded event fires at first place so color:red overrides color:black applied by media query.

Now document.ready event is fired at Second place when DOM is ready so color:blue overrides color:red.

And load event is fired at Third place because image is not loaded when DOM was ready and so color:yellow overrides color:blue after image is loaded,

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

window.onload与$(document).ready()

jQuery .ready()与window.onload

代码在使用window.onload而不是document.ready时有效吗?为什么?

为什么window.onload事件在$(document).ready之前发生?

window.onload与document.onload

流星js中jQuery中window.onload(function(){})和$(document).ready(function(){})的等效函数是什么?

$(document).ready(function()VS $(function(){

$(document).ready中有多个$(document).ready和$(window).load

window.onload与body.onload与document.onready

jQuery Document ready函数与窗口onload函数冲突

$(document).ready(function(){}); vs脚本在页面底部

window.load在document.ready之前执行

$(window).load()和$(document).ready()函数之间的区别

$(window).load和$(document).ready有什么区别?

可以在 window.addEventListener 中使用 $(document).ready

嵌套$(document).ready()和$(window).load()事件之间的区别

改善$(document).ready和$(window).resize并使其更紧凑

$(window).resize()和$(document).ready()计算不同的值

$(window).on('load')在jQuery(document).ready()中未正确调用

在 document.ready 中的 window.load 事件上的 domlistener

将变量从 $(document).ready(function(){} 传递给 $(window).load(function() {}

jQuery CSS不适用于document.ready

window.onload与<body onload =“” />

onload()和$ .ready之间的区别?

在以下时间运行函数:window.scroll和window.resize和document.ready

为什么我的CSS Media Queries无法正常工作?

如何使用 CSS Media Queries 将图像置于文本下方?

如何将 $(document).ready (function) 更改为 onload 特殊标签,以便将变量传递给 js 函数

google.maps.event.addDomListener(window,'load',function(){})vs window.onload = function(){}