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.

8
  • Do you mean to execute JS appending from the ajax content, or execute a function after the ajax call and appending is done? Commented Aug 18, 2010 at 16:05
  • where is someFunction defined? Commented Aug 18, 2010 at 16:06
  • someFunction is defined in a .js files in the page that calls the ajax request. There's HTML loaded into an DIV that calls the function Commented Aug 18, 2010 at 16:09
  • you are loading <script> AND html into a <div> and expecting 'document' ready to trigger? Commented Aug 18, 2010 at 16:14
  • Looks like someFunction may have problematic code. Can we see it? Commented Aug 18, 2010 at 16:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Why should I do that, if the javascript is already present in the document that call the new HTML
You should look which data you have inside of success handler of jQuery.ajax. Probably you want use jQuery.load (see 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.
hmmm, oké I'll try that. So its not possible?
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.
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.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.