You need to invoke javascript code:
- after loading page
- when resizing browser window
This code works for the first case and for some reason does not work for the second:
$(function() { $(window).resize(doSomethingUseful()); }); function doSomethingUseful() { $('body').append('<div>Handler for .resize() called. ' + document.body.clientWidth + '</div>'); }
But this one works for the second case, but does not work for the first:
$(function() { $(window).resize(function() { $('body').append('<div>Handler for .resize() called. ' + document.body.clientWidth + '</div>'); }); });
See the two test examples below - you can copy them, save and run.
What code will work for both cases?
Updated from comments.
Test Example 1:
<html> <head> <title>resize</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript"> $(function() { $(window).resize(doSomethingUseful()); }); function doSomethingUseful() { $('body').append('<div>Handler for .resize() called. ' + document.body.clientWidth + '</div>'); } </script> </head> <body> <div>hello world :)</div> <hr> </body> </html>
Test Example 2:
<html> <head> <title>resize</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript"> $(function() { $(window).resize(function() { $('body').append('<div>Handler for .resize() called. ' + document.body.clientWidth + '</div>'); }); }); </script> </head> <body> <div>hello world :)</div> <hr> </body> </html>