PHP echo Outputting JavaScript - script does not have same scope
I am currently designing an HTML5 Canvas application and am using an Image Uploader so that users can upload local images to the server and then modify the image on canvas (as a number of manipulations to external sources will give you a security exception).
I have some JS included that goes something like this:
<script type="text/javscript">
var editImage = new Image; // Global scope开发者_如何学运维; not in a function
function setupCanvas() {}
</script>
Once the image has been successfully loaded I echo something like this:
echo '
<script type="text/javascript">
alert("'.$upload_image.'");
window.editImage=new Image();
window.editImage.src=\''.$upload_image.'\';
alert(window.editImage.src);
setupCanvas();
</script>'
Now the output I get from the alert() statements are $upload_image. But when I check FireBug, it'll say that the editImage var that I have declared GLOBALLY in a separate script is still the ORIGINAL image source, NOT $upload_image. Furthermore, it'll say the setupCanvas() method that I have called will be UNDEFINED, although it is in the GLOBAL scope.
I am assuming when I do this with PHP there must be some scope issues - is there a way around this?
Sorry code block in comments was unreadable, this is how I got it working with JUST javascript:
$(document).ready(function() {
editImage=new Image();
editImage.src='http://graffpic/uploads/fracture_1292689775.jpg';
console.log(editImage.src);
setupCanvas();
});
Only problem is when I output it with PHP it will say $ is UNDEFINED - so this is most likely a loading issue - can anyone give me a bit more detail on this and what to do? I COULD import the jQuery library AGAIN, but that seems clunky; either that or I could import the code fragment that is needed for $(document).ready() which is again quite clunky. Any help?
Ah! I hope it's actually your problem too. Not just a typo in your post, cause I've been scratching my head for ages with some code based on your example. It's not simply that your first code block reads,
<script type="text/javscript">
Not
<script type="text/javascript">
is it? (Note the missing 'a' in 'javascript' from the first line.)
精彩评论