-2

I have an ajax call and I want to use result of this ajax on another page.

//This is main.js file

function myFun(){
    var my_user = "Stas";
    var itemsUrl = "myxmlwithusers";
    var user_found = false;
    $.ajax({
         url: itemsUrl,
         method: "GET",
         headers: { "Accept": "application/json; odata=verbose" },
         cache: false,
         async: false,
         dataType: "text",
         success: function(data){
            var jsonObject = JSON.parse(data);
            results = jsonObject.d.results;
            $(results).each(function(){ 
                if($(this)[0].user == my_user){
                     user_found = true;
                }
            });
         },
         error:function () {
              console.log("Error");
          }
        }
    );
}

// This is execute.js file

myFun();
if (user_found == true){cool}

I need true or false for // user_found. Let say I will put in the file execute.js my function myFun() and in main.js my user is fetch with "$(this)[0].user" and I need to receive "true" in execute.js

Thanks!

prince
  • 449
  • 2
  • 5
  • 6

3 Answers3

0

1st: simply in html before including any js files

<head>
   <script>
      var user_found = true; // or false if you like
   </script>
   // include main.js and execute.js
</head>

now you can call user_found anywhere in js files without var .. even it change in any js files functions it will change in others

Simple Example

2nd: If you include main.js before execute.js you can use it like

var user_found = false;
function myFun(){
    var my_user = "Stas";
    var itemsUrl = "myxmlwithusers";
    $.ajax({
         url: itemsUrl,
         ....................

Simple Example

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
0

The problem is that $.ajax call is asynchronous, a.k.a you check for value of user_found before it is set. try this approach

function myFun(callback){
    var my_user = "Stas";
    var itemsUrl = "myxmlwithusers";
    var user_found = false;
    $.ajax({
         url: itemsUrl,
         method: "GET",
         headers: { "Accept": "application/json; odata=verbose" },
         cache: false,
         async: false,
         dataType: "text",
         success: function(data){
            var jsonObject = JSON.parse(data);
            results = jsonObject.d.results;
            $(results).each(function(){ 
                if($(this)[0].user == my_user){
                     user_found = true;
                }
            });
            if(typeof callback === "function"){
               callback(user_found);
            }
         },
         error:function () {
              console.log("Error");
          }
        }
    );
}

Then you can use it like

myFun(function(result){
    if (result == true){
        //cool
    }
});
Martin Hlavňa
  • 648
  • 7
  • 20
0

Consider using HTML5 Local Storage. Stored values should be accessible by every page served on your domain.

// Store
localStorage.setItem("user_found", "true");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("user_found");
Rian
  • 21
  • 2