Help to understand - you need to transfer a variable from a script to another script. I try to do this: I set the global variable

var razmerVes = ""; 

I assign a variable a new value on click

 $(document).ready(function() { $("#el_<?=$arOneValue?>").click(function() { razmerVes = "<?=$x?>"; }); }); 

Next, I want to get it in another script

 pec_goods[0] = window.razmerVes; // Ширина/Длина/Высота/Объем/Вес 

But the razmerVes variable razmerVes empty.

  • The script in which I want to get the value is just below the code on the same page - DENVOL
  • You need to remove the var and declare also window.razmerVes = ""; - Mr. Black
  • So also tried. It is not transmitted and all. - DENVOL
  • Do not touch the window object, it has nothing to do with your program. Get your object in your scope and use it. - Yura Ivanov
  • one
    @ilyaplot, there may be options - Dmitriy Simushev

1 answer 1

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.

  • To beat the hand, just write 'use strict'; . - Yura Ivanov
  • Thanks for the detailed answers. But how else to get, without declaring the variable global, to get the value of the variable in another place of the code? - DENVOL
  • @YuraIvanov, I’m more about administrative rather than technical measures - Dmitriy Simushev
  • one
    Using global variables is an error; use strict helps to see this error with the naked eye. In the meantime, storing some of your data in the window, document, and other system objects is beyond evil, only administrative measures. - Yura Ivanov
  • one
    @DENVOL, alas, it is not interesting to me. Try to contact the freelancers exchange. If you have specific questions, feel free to ask them on ru.SO (of course in the form of new, individual questions). - Dmitriy Simushev