jeudi 25 février 2016

global variables in javascript not working?

I have two functions in javascript. one is change_BASE_ID the other is function Display_img(a,b). change_BASE_ID is called on mouseqlik which internally calls display_img. On mouse over ,Display_img is called. So Display_img is used in two ways. There us variable base_id_mitali in change_BASE_ID function which i want it global. so even on mouse over when Display_img function is called independently, it should make use of the value of that variable. If onclick funation was never clicked the value in base_id_mitali should be 01 or else if it was clicked it should be the one set prevviously.

function change_BASE_ID(base_ID,cursor_ID) {                            // THIS IS WHEN MOUSE IS CLICKED
        //var curr_base_id = 'ch01ch01.png';
        var start_name = "ch";
         base_id_mitali= "01";
        var bsid=document.getElementById('image').src;
        //var bsidlen=bsid.charAt(bsid.length-6);
        var bid1=bsid.charAt(bsid.length-6);
        var bid2=bsid.charAt(bsid.length-5);
        document.getElementById("mitali").innerHTML = "trying to get base id ".concat(bid1).concat(bid2);
        base_id_mitali=concat(bid1).concat(bid2);
        
        var a = base_ID;
        var b = cursor_ID;
        
        var temp_res1 = start_name.concat(base_id_mitali);

        var temp_res2 = temp_res1.concat("ch");

        var temp_res3 = temp_res2.concat(b);
        
        var final = temp_res3.concat(".png");
        

        curr_base_id = final;

        document.getElementById('image').src = final;
        Display_img(base_ID,cursor_ID);
        
        document.getElementById("demo").innerHTML = "clicked on ".concat(final);
        //setbaseid(base_id_mitali);
        
}

  function Display_img(a,b) {
   
        var start_name = "ch";
        //document.getElementById("globalvar").innerHTML = "trying see global variable value ".concat(base_id_mitali);
        var temp_res1 = start_name.concat(a);

        var temp_res2 = temp_res1.concat("ch");

        var temp_res3 = temp_res2.concat(b);
        
        var final = temp_res3.concat(".png");
        
        document.getElementById("demo").innerHTML = final;
        
        
        document.getElementById('image').src = final;   
  }


  
</script>
</html>



Aucun commentaire:

Enregistrer un commentaire