0

Running Js code when screen size is in a certain size

I'm trying to run some javascript code when the screen size is in a certain size... Well i tried with jquery: $(window).width(), well it does work but i have to refresh with page for it .... Please help('Javascript')

28th Nov 2018, 6:45 AM
Edwing123
Edwing123 - avatar
6 Answers
+ 1
CalviŐ˛ such an intereting thing to try ... Thansk for answering bro
28th Nov 2018, 7:57 AM
Edwing123
Edwing123 - avatar
0
Have you try to run the code in window.onload ?
28th Nov 2018, 6:52 AM
Taste
Taste - avatar
0
Use matchMedia function to detect: var mql = window.matchMedia('(max-width: 600px)'); mql.addListener(function(e) { if(e.matches) console.log('screen smaller or equal to 600px') ; else console.log('Screen size larger than 600px'); } ); https://code.sololearn.com/WBKZ4jpDuwzB/?ref=app
28th Nov 2018, 7:11 AM
CalviŐ˛
CalviŐ˛ - avatar
0
Taste. No, I haven't but i'll try ... Thanks for helping me bro
28th Nov 2018, 7:55 AM
Edwing123
Edwing123 - avatar
0
You could also wrap it inside resize method $(window).resize(function() { if($(this).width() > 600) return; alert(”smaller than 600”); });
28th Nov 2018, 9:40 AM
Toni Isotalo
Toni Isotalo - avatar
0
Toni Isotalo, I tried that too But it didn't work for me. thanks for answering $(document).ready(function(){ $(window).resize(function(){ // code here }); });
28th Nov 2018, 2:10 PM
Edwing123
Edwing123 - avatar