Asynchronous jQuery-Prototype conflict

Asked

Viewed 105 times

0

Well I hold the use of the two libraries and so that there was no conflict I chose to use the jQuery.noConflict() however at the same time I tried to keep the use of dollar but my code breaks due to "asynchronity" of the code.

<script type='text/javascript' src='//code.jquery.com/jquery-2.1.3.js'></script>
<script>jQuery.noConflict();</script>

<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>

<script type='text/javascript'>
(function($){
    $("body").css('background-color', 'red');

    var x = 1;
})(jQuery);

console.log(x);
</script>

see that X is not printed, there are ways to solve?

  • What’s asynchronous about it? I guess nothing.

1 answer

1


It does not print because the x is outside the scope (must be giving an error, no?).

You need to declare x in the outermost scope:

var x = 0; // DECLARE AQUI
(function($){
    $("body").css('background-color', 'red');
    // AQUI TIRE O var
    x = 1;
})(jQuery);

// Funciona!
console.log(x);
  • What do you mean? I don’t understand.

  • Is there any way to do this except with the prototype?

  • Give a concrete example of what you are trying to do, otherwise it is difficult to say anything.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.