views:

61

answers:

5

Hello. I have the following piece of code

<a onclick="$('#result').load('codes/test.html');$('#result').show();" >XHTML code</a>

it loads perfectly the content of test.html in the result div and also make it visible. Until this point all are good. When I try to add a function prettyPrint() which apply some modifications on the text and change the code to the following:

<a onclick="$('#result').load('codes/test.html');$('#result').show();prettyPrint();" >XHTML code</a>

I can't make the prettyPrint() to work, instead if I add onmouseout="prettyPrint(); it works when I take off my cursor. I don't have experience with javascript and jquery so I don't know what is the real problem, so I would appreciate any help on how to make prettyPrint to work onckick.

A: 

You can do the following:

$('#result').show("fast",function(){ prettyPrint();});
PIM
+2  A: 

If you are using jQuery why do you bother with inline Javascript? Why not:

<script type="text/javascript">
$(function(){
    $('#myLink').click(function(){
        $('#result').load('codes/test.html', function() {
            $('#result').show();
            prettyPrint();
        });

        return false;
    });
}):
</script>

<a id="myLink" href="#">XHTML code</a>

Edit:

Fixed code so it calls prettyPrint() when load callback is called.

rochal
-1: this does not solve the problem
Fyodor Soikin
why does it not solve the problem?
rochal
Yep, -1 too, even if I think like you : "OH MY GOD STOP INLINE JS !"
Clement Herreman
@rochal : as the load() is asynchronous, the only reason prettyPrint doesn't work is that it is executed, while #result is still empty.
Clement Herreman
@clement, Hmm... good point. PrettyPrint() should be added as a callback
rochal
@Fyodor Soikin, @Clement Herreman - check out my modified code.
rochal
Much better ! =)
Clement Herreman
thanks, this made my code cleaner.
Sotiris
+2  A: 

The load is asynchronous. When prettyPrint is called, the text might not be there yet.

<a onclick="$('#result').load('codes/test.html', function(){prettyPrint();$('#result').show();});" >XHTML code</a>

load() takes a second argument which is a function to call when the load is done. I moved the prettyPrint and the show to there.

Lou Franco
thanks, this solved my problem! :)
Sotiris
A: 

You should use a callback function:

<a onclick="$('#result').load('codes/test.html', function() {prettyPrint();}).show();" >XHTML code</a>
graycrow
A: 

Your problem is that you misunderstand how .load() works. It does not block until the data is loaded. Instead, it only starts loading and returns immediately. Therefore, your prettyPrint function gets called when the data is not loaded yet.

To fix this, pass a callback function to .load. That callback will be called when the load completes:

$("#result").load( '...',
    function() { 
        $("#result").show();
        prettyPrint();
    }
}
Fyodor Soikin
Or you can make `load()` works synchronously... Just joking ^^
Clement Herreman