I have php file, in this file I have this code:
<script language="JavaScript" type="text/javascript" src="jquery.js"></script>
<script language="JavaScript">
$(document).ready( function () {
var myvar = <?php echo json_encode($myvar); ?> ;
});
</script>
<script language="JavaScript" type="text/javascript" src="costum.js"> </script>
and in costum.js file I have code:
$(document).ready( function () {
alert(myvar );
});
this not working, error consol returns “myvar is undefined”
if in php file I write this (that is, without “document.ready”)
<script language="JavaScript">
var myvar = <?php echo json_encode($myvar); ?> ;
</script>
in costum.js file, code alredy is working. Please tell why this happened?
try with
your variable has to be declared as global (or in other words, in the outer scope) to be viewed from both
document.readyfunctions.As a side note
languageattribute is not necessary. Eventypeis not necessary (if you’re usinghtml5doctype)