LeTung LeTung - 1 year ago 74
CSS Question

Jquery launch as soon as the page loads

I have a jquery.

$(window).on('load resize', function() {
var dm3Width = $('.dm3').width()

Demo Here: https://jsfiddle.net/veusr0xn/

It changes
, based on the width of
. But it runs only after I adjusted the size of the window. How does it run as soon as the page loads?

Thank so much.

Answer Source

Define your query under $( document ).ready(function() Scope so when document will load your query will automatically work

   $( document ).ready(function() {
     var width=$(".dm3").width();
     $(window).resize(function() {
       var dm3 = $('.dm3').width();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download