javascript - undefined value cause my whole function not working -


i have defined global javascript function in masterpage

masterpage

<script> if(typeof data !='undefined'){ function dosmth{ if(data[a]) //do } } </script> 

homepage

<script> var data = { fruit : "apple" } </script> 

detail

var data not defined in page 

before added typeof data !='undefined', work fine, can call apple in homepage received javascript error detail because data undefined, prevents continuing receive javascript error, added typeof data !='undefined', somehow whole function not working, why happen?

change masterpage script as

<script>     function dosmth(){         if(typeof data !== 'undefined'){             if(data[a]){             //do             }         }     } </script> 

ideally should pass parameter function rather using global variable. suggest use

<script>     function dosmth(data){         if(typeof data !='undefined'){             if(data[a]){             //do             }         }     } </script> 

and call function like

<script>     var data = {        fruit : "apple"     }          dosmth(data); </script> 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -