ReferenceError when calling a regular javascript function as a callback from jQuery.get

I am trying to use a javascript function defined outside of $ (document) .ready (function () {}); as a callback to request $ .get (). However, firebug shows:

ReferenceError: temp is not defined $.get('twitter.php', function(data){temp(data)}); 

Here is the relevant code:

 <script src="http://code.jquery.com/jquery-1.8.2.js"></script> <script type="txt/javascript"> function temp(data){ alert(data); } </script> <script> $(document).ready(function() { $.get('twitter.php', function(data){temp(data)}); }); </script> 

twitter.php returns data.

+4
source share
4 answers

You have a script type set to 'txt/javascript' , if I am not mistaen, it should be 'text/javascript' , you will also need a semicolon after your function.

 <script type="text/javascript"> function temp(data){ alert(data); }; </script> 
+1
source

Remove type="txt/javascript" or change it to text/javascript .

+3
source

put

  function temp(data){ alert(data); } 

above

 $(document).ready(function(){ }); 
+2
source

You have a small syntax error that invalidates the script, type="txt/javascript" :

 <script type="text/javascript"> function temp(data){ alert(data); } </script> 
+1
source

Source: https://habr.com/ru/post/1446858/


All Articles