Tuesday, May 22, 2012

run javascript code on AJAX request

i have a upload dialog that allow me to upload files with plupload, but i have a problem,
when i load content directly, it work, but when i load it with an ajax request, javascript code will execute, but dom elements not ready yet, when i make a small delay with timeout, it worked, how i can run javascript code , after ajax dom ready?



blew code load with ajax request:



    <form enctype="multipart/form-data" method="POST" action="admin.php?do=media&action=file-upload">

<div id="uploadForm">

<div id="fileList">



</div>

<a href="#" id="selectFile">[Select File]</a>
<a href="#" id="uploadFile">[Start Upload]</a>

</div>

</form>

<script type="text/javascript">
// $(function(){}); not working when data loaded with ajax request
var ajaxUpload = new plupload.Uploader({

runtimes: 'gears, html5, flash',
url: 'upload.php',
browse_button: 'selectFile',

});


ajaxUpload.bind('Init',function(param){
console.log(param);
console.log($('selectFile'));
});

$('#uploadFile').click(function(){
alert('Something');
});

ajaxUpload.init();


</script>




No comments:

Post a Comment