0

I have a lot of scripts on my page:

Example:

<script>
  window.addEvent('Updated', function(data) {

    /*I'm using a mootools javascript framework and my string below contain an error: 
      Uncaught TypeError: Cannot call method 'empty' of null*/ 

    var container = document.getElement('cart_contents_data'); 
    container.empty();

  });
</script>

This error break all other code (script blocks). I'm looking for the way to isolate script blocks (each other).

I'm tried something like this:

    <script>
          try {

             window.addEvent('Updated', function(data) {

               /*I'm using a mootools javascript framework and my string below contain an error: 
                 Uncaught TypeError: Cannot call method 'empty' of null*/ 

               var container = document.getElement('cart_contents_data'); 
               container.empty();    
             });

          } catch(err) {console.log(err)}

    </script>

But this solution doesn't help me. Thanks.

1 Answer 1

1

Try this code instead:

window.addEvent('Updated', function(data) {
    try {
        var container = document.getElement('cart_contents_data'); 
        container.empty();    
    } catch(err) {
        console.log(err);
    }
});

Or even better, to avoid exceptions at all:

window.addEvent('Updated', function(data) {
    var container = document.getElement('cart_contents_data'); 
    if (container != null) {
        container.empty();    
    }
});

The problem is that the code window.addEvent(....) only registers an event handler. The actual event occurs latter, and is probably fired by the javascript framework. The exception occurs when the event is fired, not when being registered, which causes your try-catch block not to work at all.

Moving the try-catch block inside the function is a possible work-around, but in your case, it seems that the cause is trivial enough to avoid it with a simple if check. It gives better performance and improves code readability.

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

7 Comments

I know your code work. But I need solution to isolate modules code. Is it possible?
What do you mean by modules. Do you use requirejs or any AMD module framework for Javascript? If so, add this to your question please (use the edit button)
No just <script></script> constructions.
@user889349. see my updated answer. Please, do not use the word modules, as this is incorrect terminology. Module is not a script enclosed with <script> tags, it is usually called script block. Modules refer to programmatic entities, usually build with the help of module framework (google curl, requirejs or javascript AMD modules). In your case, you have to fix your error one by one, there is no way to globally do a try-catch on everything, this is how javascript works.
This will allow you to call some code in case of error. Still the execution will stop as that code is done, and the rest of the script will not work. You must individually address each error, until all get fixed
|

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.