tags:

views:

50

answers:

2

I load some HTML code into a div with the .load function af jQuery that contains some code I want to execute;

$.ajax({
    type: "GET",
    url: url,
    dataType: "html",
    success: function(data){
        //.....
   }
 });

After loading the ajax request the alert(); function works, but the the someFunction won't.

$(document).ready(function(){
    alert('TEST'); //this works
    someFunction("#debug",'var');//this doesn't
});

How can I execute this function from an Ajax call

A function executed as <a onclick="someFunction()" won't work either.

A: 

Probably you should use jQuery.getScript to loading some javascript from the server and execute it.

UPDATED: In the most cases one load only the pure HTML fragment with respect of jQuery.ajax. Binding of elements to some javascript functions one do inside of success handle. All functions which one use in any event handler one loaded before (with <script> in the <head> block) on the main page (on the page which call jQuery.ajax). Then all look very clear and dynamic loading of any scripts is not needed.

Oleg
Why should I do that, if the javascript is already present in the document that call the new HTML
waterschaats
You should look which `data` you have inside of `success` handler of `jQuery.ajax`. Probably you want use `jQuery.load` (see http://api.jquery.com/load/). `jQuery.ajax` will be **not** used for page redirection, but for inserting or modifying some parts of existing HTML page. So `<head>` part of html page will not be loaded. Moreover it is better to prepare html code **fragments** which will be loaded per ajax instead of using full existing html pages.
Oleg
hmmm, oké I'll try that. So its not possible?
waterschaats
Well, I do not load comple pages I just load some HTML without the head, body etc... the loaded HTML just call the some javascript functions.
waterschaats
all is possible. But you should load html code fragment with `jQuery.load` and then either had already all scripts loaded before or load and execute the code per `jQuery.getScript` after the new html already loaded. In general you should not use existing full html pages, but **prepare** html fragments and seldom javascript code specially for loading per ajax.
Oleg
oké, thanks for your explanations.
waterschaats
The html which you load per ajax should have no `onclick="someFunction()"` construction. You should use `jQuery.bind`, `jQuery.live` etc instead.
Oleg
$('a#foo').bind('click', function() {alert('<a> with id="foo" are clicked');}); (see http://api.jquery.com/bind/ for more examples) or $('a#foo').live('click', function() {alert('<a> with id="foo" are clicked');}); The main advantage of `jQuery.live` is that you can attach a handler to an element which is not yet exist (it will be loaded per ajax for example later). See http://api.jquery.com/live/ for more explanation.
Oleg
Thanks fot showing me that wat I wanted is not a good approach! I've learnd something today!
waterschaats
You welcome! I am glad to help you.
Oleg
A: 

Why wouldn't you use the callback function that's already available in your ajax call?

$.ajax({
type: "GET",
url: url,
dataType: "html",
success: function(data){
    alert('TEST'); //this works
    someFunction("#debug",'var');//this doesn't
}});

I would take a closer look at how you are architecting the code

chromaloop