fiacobelli
fiacobelli

Reputation: 1990

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

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

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

Here's 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 does return data.

Upvotes: 4

Views: 1022

Answers (4)

Midincihuy
Midincihuy

Reputation: 21

put

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

above

$(document).ready(function(){

});

Upvotes: 2

OneChillDude
OneChillDude

Reputation: 8006

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

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

Upvotes: 1

Travis J
Travis J

Reputation: 82297

You have a small syntax error which causes the script to be invalid, type="txt/javascript" should be:

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

Upvotes: 1

Sampson
Sampson

Reputation: 268424

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

Upvotes: 3

Related Questions