0

I've got this problem. I open my jQueryUI dialog window, but I need to call some additional function on that. The point is, that content of the dialog is loaded by AJAX and I need to load any other content using AJAX as well (I have some kind of list on that and since the list may content huge amount of items, I need to split it into pages, into smaller amouts of data) Here is my code:

<script type="text/javascript">
    $(function() {
        loadObjects(1);
    });
    var loadObjects = function(page) {
        // do something
    };
</script>

My console keep telling me? TypeError: 'loadObjects' is not a function. But when I try to run this code not in the jQueryUI dialog (but on plane page), there is obviously no problem.

When I googled this error I only found out, that jQuery may be blocking any JS code on purpose, because of some IE explorer error.

Can anybody help me?

Thanks !

3 Answers 3

3

Put the loadObjects function declaration first.

<script type="text/javascript">
    var loadObjects = function(page) {
        // do something
    };
    $(function() {
        loadObjects(1);
    });
</script>

What you could (and probably should) be doing to avoid this is placing the function definition for loadObjects() into a separate JS file, then including that file in the page before you use anything from it. (The main exceptions to this, of course, are when you are unable to load JS files in the page, or when you need to generate the function definition dynamically, among other rare situations.)

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

1 Comment

Thank alot! Totally helped to switch the definitions (first define function loadObjects and then the jQuery $(function() {}). And I actually do not want those simple JS functions in separate files. I have a lot of modal views to dialogs and the separate file would be a bit confusing, while keeping all that content. But thanks again!
1
$(function() {
    var loadObjects = function(page) {
    // do something
    }

    loadObjects(1);        
});

Your function should be defined. once you define it, call it.

Comments

0

When you use a jQuery dialog, you first need to be sure that the content is loaded. To achieve this, you can use the open event handler in your dialog initialization :

$( ".selector" ).dialog({
  open: function( event, ui ) {
      var loadObjects = function(page) {
       // do something
      };
      $(function() {
        loadObjects(1);
      });
  }
});

1 Comment

Thanks, but actually I can't do that. I have a lot of modal views to dialogs, so I would to this definition to all views. But anyway thank you!

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.