-2

Below, i have included a screenshot form the F12 view in chrome.

My problem is: Uncaught ReferenceError: callModal282876547 is not defined. The other errors are not an issue.

I don't know if this is relevant, but the code that is shown was loaded into the page by an ajax call (including the javascript).

The error occurs when i click the button, and tells me the function is not defined, but it is clearly present in the code? What can cause this, and how would i solve this problem?

enter image description here

Ward Beullens
  • 393
  • 5
  • 18
  • 1
    When you say the code being displayed is being loaded over an AJAX call, are you saying that the `script` tag was loaded over AJAX and injected into the body? If so, please provide that code. There are tricks in getting new JS to load in and execute. – JAAulde Aug 21 '15 at 14:25
  • This is somewhat unrelated to your question, but please review if you **really** need a separate callModal function for every modal. – Florian Wendelborn Aug 21 '15 at 14:31
  • An image is helpful, but you should also include relevant code, so that for future readers, the content of the question doesn't rely only on external sources. – Artur Filipiak Aug 21 '15 at 14:33
  • @JAAulde , yes how would i make the the browser do that? – Ward Beullens Aug 21 '15 at 14:44

1 Answers1

0

The browser doesn't know anything about the new function you brought with ajax response. So, you must eval() your declaration code returned by the Ajax callback and make it available to DOM.

PersyJack
  • 1,736
  • 1
  • 21
  • 32
  • Yes, i figured that this is indeed the problem. The new code that i smuggled in with ajax might contain a document ready function , (or several ones). Can i call all of these when the ajax call is done? Funny thing is that for some reason the first time that i do an ajax call the document ready function is being executed , but the second, third,... times it is not being executed – Ward Beullens Aug 21 '15 at 15:26
  • Assuming 'response' is your ajax response, something like that should work: `var scripts = []; response.forEach(function(key, element){ scripts.push(element.innerHTML); }); scripts.forEach(function(key, element){ eval(element); });` – PersyJack Aug 21 '15 at 16:27