gevensen Posted June 3, 2015 Share Posted June 3, 2015 I have some routines I use over and over Is there a way to put the error section in a file of its own For example I use a routine below <script type="text/javascript"> $('.MYTRIGGER').live('change', function() { var MyData=''; $("#jq_tab").val('0'); var NameData=$(this).attr('name'); var IdData=$(this).attr('id'); MyData=MyData+'CID='+NameData;; MyData=MyData+'&Field='+IdData; MyData=MyData+'&FieldValue='+$(this).val(); $.ajax( { cache:false, timeout:28000, url : 'MyPhp.php', type : 'post', data : MyData, success : function( resp ) { $("#jq_tab").val('1'); return(false); }, error: function(xhr, status, error) { var stat = $("#jq_tab").val(); $("#main_page_div").unmask_div(); if(stat!='1'){ if (xhr.status === 0) { alert('Unable to connect to Network'); } else if (xhr.status == 404) { alert('Requested page not found. [404]'); } else if (xhr.status == 500) { alert('Internal Server Error [500].'); } else if (exception === 'parsererror') { alert('Requested JSON parse failed.'); } else if (exception === 'timeout') { alert('System Timeout (Internet Congestion or server slow)'); } else if (exception === 'abort') { alert('Ajax request aborted.'); } else { alert('Unspecified Error #' + xhr.responseText); } } return false; } // eof error function }); }); </script> Since the below is common error: function(xhr, status, error) { var stat = $("#jq_tab").val(); $("#main_page_div").unmask_div(); if(stat!='1'){ if (xhr.status === 0) { alert('Unable to connect to Network'); } else if (xhr.status == 404) { alert('Requested page not found. [404]'); } else if (xhr.status == 500) { alert('Internal Server Error [500].'); } else if (exception === 'parsererror') { alert('Requested JSON parse failed.'); } else if (exception === 'timeout') { alert('System Timeout (Internet Congestion or server slow)'); } else if (exception === 'abort') { alert('Ajax request aborted.'); } else { alert('Unspecified Error #' + xhr.responseText); } } return false; } // eof error function Is there a way to store the error function in a common file? Quote Link to comment https://forums.phpfreaks.com/topic/296632-reusing-common-content/ Share on other sites More sharing options...
Psycho Posted June 3, 2015 Share Posted June 3, 2015 (edited) Just use a script tag to include a file with the error function: <script type="text/javascript" src="path/nameOfFile.js"></script> Edited June 3, 2015 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/296632-reusing-common-content/#findComment-1513131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.