The content of the function passed as the $(document).ready argument is asynchronous . This means that your variable gets the correct value, but at the wrong moment in time. In other words, you most likely use the razmerVes variable before assigning a value to it.
If you want to transfer data through a global variable asynchronously, use Promises :
window.razmerVes = new Promise(function(resolve) { $(document).ready(function() { $("#target_el").click(function() { // Возвращаем значение через Обещание. resolve('foo bar baz'); }); }); });
Now, in the script where you need to get the value of razmerVes you can do the following:
window.razmerVes.then(function(value) { // Выведет "foo bar baz" alert(value); });
And here is a working example on JSFiddle.
However, something tells me that you do not need to work with the variable razmerVes in an asynchronous manner. If this is the case, then you can remove the setting of the variable value from the function:
window.razmerVes = "<?=$x?>"; $(document).ready(function() { $("#el_<?=$arOneValue?>").click(function() { // Другие асинхронные действия }); });
Comment:
In general, for global variables it is necessary to beat hands. Situations where they really need to be used can be counted on the fingers of one hand, and your case does not look like one of these.
varand declare alsowindow.razmerVes = "";- Mr. Black