There is html that is formed once when the page is loaded, then changes are made by the same js , and at a certain resolution, you must reset the entire generated html , resize event.

The essence of the question, how to run the script once?

 function obnovitStranicu() { if ($(window).width() < 470) { location.reload(); } } $(window).resize(function() { obnovitStranicu(); }); 

  • Something strange decision ... Think about whether it all turns out correctly. And to execute once, you need not write it to the resize event handler, but write it as a function separately and, when necessary, simply call it. - VostokSisters
  • You can run it if it does not apply to reloading the page, otherwise your entire code will be re-executed. If you still need to run the script after the reboot, you will have to write to localStorage - Vasily Barbashev
  • You need to do not just reload, but pass a parameter to the server (you can use a cookie or request parameters) that you are now performing a controlled reload, and the server part in this case should give the page without this piece of code. Well, at the same time, decide what is "once" and keep in your session data, for example, that such a reboot was performed and it is still possible to remember the permission on which it was performed, to enable this code again if the permission changes - Mike
  • @Mike I made both options purely on the client side, without loading the server, but you correctly noticed for the creator of the question that it is not clear what "once" - Vasily Barbashev
  • Immediately apologize, it is difficult to convey the essence. Similar to the intended example north2.net/projects/?browser=full In my example, the code is called until the value is smaller, you can make an equal sign! but he sometimes slips .... And it does not always work. - Egor

2 answers 2

If you want to check the scale of one page / each individual

 (function() { var onResizeHandler = function(event) { if (this.innerWidth < 470) { console.log('Делаем что-то 1 раз'); // Действия $(window).unbind('resize', onResizeHandler); } }; $(window).bind('resize', onResizeHandler); })(); 

If you need to check once, when used ever, and never again

 (function() { // localStorage.clear() // Проверяем была ли инициирована переменная выполнения нашего условия var isExecuted = localStorage.getItem('isExecuted') === 'true'; // И если нет, создаем обработчик и подписываемся на событие if (!isExecuted) { var onResizeHandler = function(event) { console.log(this.innerWidth) if (this.innerWidth < 470) { console.log('Делаем что-то 1 раз'); // Условие совершилось, значит запишем значение в память страницы localStorage.setItem('isExecuted', true); // И удалим обработчик на текущий момент, чтобы не грузить страницу $(window).unbind('resize', onResizeHandler); } }; $(window).bind('resize', onResizeHandler); } })(); 

  • I thank you for your answer!

If correctly understood:

 function obnovitStranicu() { location.reload(); }; if ( $(window).width() < 470 ) obnovitStranicu(); 
  • Will your method be called once? Not. You have a strange example. When you change the screen size, nothing will happen to you. Your example does not fulfill the conditions that are required - Vasily Barbashev
  • @ Basil, the method is called once. When I wrote the answer, the question was poorly formed, and it turned out that I did not answer that way) - VostokSisters