Saturday, 15 January 2011

javascript - check if file exists, display according message, jQuery -



javascript - check if file exists, display according message, jQuery -

i need global function checks if file exists. need reuse function several methods want homecoming true or false. tried create asynchronous can't work. function:

function checkindex(){ $.ajax({ url:'upload/_3.fdt', type:'head', async: false, error: function(){ homecoming false; }, success: function(){ homecoming true; } }); }

this 1 functions calls checkindex()

$(function(){ $(document).ready( function(){ if(checkindex() == false) $('#check').html('<td><img src="img/cross.gif" /></td><td>no index present</td>'); else $('#check').html('<td><img src="img/check.gif" /></td><td>index present</td>'); }); });

how can create work?

i'd homecoming deferred object ajax function, , insert html based on wether or not ajax function failed or not :

function checkindex(file){ homecoming $.ajax({ url : file, type:'head' }); } $(function(){ checkindex('upload/_3.fdt').done(function() { $('#check').html('<td><img src="img/check.gif" /></td><td>index present</td>'); }).fail(function() { $('#check').html('<td><img src="img/cross.gif" /></td><td>no index present</td>'); }); });

if pass filename check function, it's reusable well.

javascript jquery

No comments:

Post a Comment