6

I am trying to execute things on window.resize, but I can't even get alert to work more than once. All of the code I have is this:

HTML:

<head>
    <meta charset="utf-8">
    <script src="jquery-1.11.1.min.js"></script>//I have this library in this html's file directory
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function(){
      window.resize(alert("yolo"));
    });
  </script>
</head>
<body>
...
</body>

I have also tried several variations:

window.resize(...)

window.on("resize",...)

window.one("resize",...)

$(window).resize(...)

$(window).on("resize",...)

$(window).one("resize",...)

None of them work. Is there anything else I haven't tried?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Not sure why it does not work. Worked for me: http://jsfiddle.net/bakgq9g7/ – karthikr Oct 06 '14 at 01:27
  • 1
    in pure JS it's `window.onresize = resizeHandler;` – myfunkyside Oct 06 '14 at 01:27
  • well with just `window` you wouldnt be able to call `.resize`, `on`, or `one` as its not a jQuery object, and for the jQuery calls it expects a function reference not the return of a function call, and in the case of the return of a alert call its `undefined` so you arent setting a callback at all – Patrick Evans Oct 06 '14 at 01:28
  • A duplicate, a duplicate.. basically a function is *not* supplied as the event handler. – user2864740 Oct 06 '14 at 01:30

2 Answers2

4

This only gets called once since the document.ready is consumed on the page load.

$(document).ready(function(){
     window.resize(alert("yolo"));
});

You would need to attach an event handler to the window window.onresize = function() { alert("yolo"); }; or

$(window).resize(function () {alert("yolo");});

Abbath
  • 582
  • 2
  • 7
  • 1
    also, probably smarter to use `console.log` instead of `alert()` if you don't want to go crazy from the popups – myfunkyside Oct 06 '14 at 01:30
  • 1
    You still have it as `$(document).ready(function(){ window.resize(alert("yolo")); });`. You want `window.resize(function () {alert("yolo");});` – Abbath Oct 06 '14 at 01:38
  • 1
    You are binding the function to the page load event, not the window resize event. – Abbath Oct 06 '14 at 01:41
0

Your alert('yolo') is getting executed at the moment it's consumed by the javascript runtime. That's why you're seeing it only once, when the page loads.

Also window.resize doesn't exist but window.onresize does :

window.onresize = function (){
    alert('yolo');
};
Yoann
  • 3,020
  • 1
  • 24
  • 34