Asynchronous jQuery-Prototype conflict

0

Well I stop using the two libraries and so that there was no conflict I opted to use jQuery.noConflict() however at the same time I tried to keep using the dollar but my code breaks due to " asynchrony "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><scripttype='text/javascript'>(function($){$("body").css('background-color', 'red');

    var x = 1;
})(jQuery);

console.log(x);
</script>

Notice that X is not printed, how can I solve it?

    
asked by anonymous 29.10.2015 / 01:02

1 answer

1

Does not print because x is out of scope (should be giving an error, right?).

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);
    
29.10.2015 / 01:19