0

I am beginner in web development. I am trying to send variable from one js file to another js file . I researched alot before posting this question. I couldn't find any particular method by which i can do this.

My script which defines variable is

function getEventData(){
var eventList = document.getElementsByName("events");
var k=0;
var url = "https://www.eventbriteapi.com/v3/events/search/?categories=";
for(var i=0;i<eventList.length;i++){
    if(k>=3)
        break;
    if(eventList[i].checked){
        if(k==2)
            url+=eventList[i].value+"";
        else
            url+=eventList[i].value+",";
        k++;
    }
}
url+="&token=************";   
}

The url variable is perfect. I want to use it in another script which is

var jsonData=getJSON('url').then(function(data)
{
 var sjd="";
for(var i=0; i<data.events.length; i++)
 {
     var url = data.events[i].name.url;
    sjd +='<h4>'+ "<br/>"+ data.events[i].name.text + '<br>'+'</h4>';
 }
 document.getElementById("events").innerHTML = sjd ;
 }
 );

As seen i want to use the url variable from first js to second which is used in getJSON function.

I found local storage and cookies as a possible way. But storage doesnt work even if i use the link stated in comment. I cant find any good resource where i can learn cookies. Also is there any other way possible?

1 Answers1

0

As suggested, use localStorage to store do the job.

localStorage.setItem("myvar", "jour json serialised string of variable");

and on the other page use it

var myvar = localStorage.getItem("myvar");

Again, deserialise on the other page. JSON.stringify is something you should know already. Also jquery has jStore plugin for this.

Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42