There is a page with a script, I need to transfer the value from this script to another script on another page that I will open next to the current page, how can this be done?

It will look like this, on the current page there is a combo-boch and a button to open the page, by clicking on the button you need to open a pre-written page with a script and transfer the parameters from the combo-box to the script on the open page. Also with this value (from the combo-box) in parallel, you need to transfer the value of variables between the scripts. How can this be done correctly?

    2 answers 2

    Well, there are cookies, there is a local storage, as already mentioned. You can also use the array GET (in the address bar).

    If, nevertheless, it is really that in some cases it is better to store the key in the cookie and to save the necessary data in the database.

    Adding data to the GET array:

    <a href="index.html?data1=0&data2=1">Ссылка</a> 

    Reading a GET array using javascript:

     var tmp = new Array(); // два вспомагательных var tmp2 = new Array(); // массива var param = new Array(); var get = location.search; // строка GET запроса, то есть все данные после ? if(get != '') { tmp = (get.substr(1)).split('&'); // разделяем переменные for(var i=0; i < tmp.length; i++) { tmp2 = tmp[i].split('='); // массив param будет содержать param[tmp2[0]] = tmp2[1]; // пары ключ(имя переменной)->значение } var obj = document.getElementById('greq'); // вывод на экран for (var key in param) { obj.innerHTML += key+" = "+param[key]+"<br>"; } } 
    • if it's not difficult tell me how to work with GET array from javascript or send a link to the necessary resource. - perfect
    • @perfect, if I'm not mistaken, then working with it on pure JS is very unpleasant. You need to get all the contents of the address bar and find the elements of the array in it. Now I will find an example and update the answer. - Telion
    • all clear. thank. Mark your way (separate from the main answer) so that it is more understandable - perfect
    • your main answer is coming, and then the answer to my comment, everything merged. just before the code write what it is GET way. for people who do not read comments. but now you pick up the minuses for the confusion in the answer - perfect
    • in this case, it makes no sense to make param array. This is a regular object. In addition, it makes no sense to initialize variables with arrays, if they are immediately rewritten afterwards - Grundy

    Through a cookie or localstorage, if you do not want to store info on the server

    Another option https://github.com/allmarkedup/purl