How to determine that the user "Turned into the window" browser and, if the window size is within 100 — 999px , send a GET request to the server?

 $(window).resize(function() { $.get("page.php", {resize: "true"}); }); 

I noticed that when working in WebKit request is sent at the slightest resizing of the window. The request should be sent once, if the window size changes within 100 — 999px and only if the window size exceeds 999px and then returns back to the zone 100 — 999px - the request is sent again.

  • You do not understand how to determine that the window was larger and so it became smaller? or do not know how to determine the size of the window? - Vartlok
  • @Vartlok, yes you are right, I managed to find an example of using resize() and insert a GET request inside how to transfer it to me from the previously asked questions on the stack. Otherwise, I’m still poorly guided in jQuery. - Plush
  • Those. Do you have a problem with sizing and memorizing it? - Vartlok

1 answer 1

In order to determine the window size, use: $(window).width() or $(window).height() depending on which parameter you need to control, or both.

Also, you need to store past window size somewhere and whether a request has already been sent in order not to peck with requests for every change when the window is already in the sizes you need. Where it is better to store in your case, I can not say, because I do not know the structure of the project and accepted practices. You can for example:

 var state = {}; $(window).resize(function() { if ($(window).width() >= 100 && $(window).width() <= 999 && state["width"] >= 1000 && state["isSent"] != true) { state["isSent"] = true; $.get("page.php", {resize: "true"}, function( data ) { state["isSent"] = false; }); } state["width"] = $(window).width(); }); 
  • And what data storage options are possible besides cookies? In the session variable, for example, but it will require a GET request to the PHP script to save the data. - Plush
  • @Plush I would think about a global object where you can save it all. But global objects are not very good, it is better to create an object before assigning a listener to the event and save everything there. - Vartlok
  • I am beginning to understand. It turns out so. Every time I change the size of the browser window $(window).resize() I check the current width of $(window).width() based on the current data and already saved I conclude to send a GET request. Can you give an example of working with объект перед назначение слушателя на эвент to save data? - Plush
  • @Plush look in the answer, I hope I haven't messed up anywhere) - Vartlok
  • Added a condition to your example. For some reason, the state["isSent"] != true does not work, that is, if the request has already been sent, then do not send again. How to check this correctly? - Plush