Conflict between a Jquery call and a js file

Asked

Viewed 372 times

0

I’m having a problem in a function using Jquery and a file I call inside my html that are giving conflict, it’s only working one, I tried using jQuery.noConflict() but apparently it didn’t work, I don’t know I used it correctly follows the script and call that are conflicting:

<script>
var $JQuery = jQuery.noConflict()
$jQuery(document).ready(function($) { 
   $(".page-scroll").click(function(event){        
    event.preventDefault();
   $('html,body').animate({scrollTop:$(this.hash).offset().top}, 1000);
    });
});
</script>  
<script src="/js/demo.js"></script>
  • What’s wrong with you?

  • The first script is to scroll the page when clicked on the menu, for some reason it does not run, and when I remove this demo.js file, the menu works normally. accessing the demo.js file it is minified, but I identified that the problem is in this part: "Document"in self&("classList"in Document.createelement("")?!Function(){"use Strict";var e=Document.createelement(""); :/

  • And there is no error message? Are you loading how many versions of jQuery? Is this a Wordpress site? can you add more details? The code of demo.js it may be useful to see also.

  • is giving me an error in creating noConflict() <script type="text/javascript"> var $Jquery = jQuery.noConflict() </script> Uncaught Referenceerror: jQuery is not defined the site is in wordpress, and I am only loading a version of Jquery

  • You got the jQuery loaded? What’s up console.log(jQuery); at first?

  • By the way! What gives console.log(typeof jQuery);?

  • Thank you very much for your help Sergio, I was able to solve it with Oeslei’s explanation

Show 2 more comments

1 answer

0


The argument passed to the callback of the method ready refers to selection, in your case document. That is, you are receiving the variable $ as a reference to document and not to the jQuery.

Probably what you want to do is something similar to the examples below:

// utilizar a variável que você criou $jQuery
var $JQuery = jQuery.noConflict()
$JQuery(document).ready(function() {
    $JQuery(".page-scroll").click(function() { /* ... */ });
});

// ou remover a variável $ do escopo global e utilizar apenas como jQuery
jQuery.noConflict()
jQuery(document).ready(function() {
    jQuery(".page-scroll").click(function() { /* ... */ });
});

// ou OUTRA biblioteca semelhante ao jQuery que use o $
jQuery.noConflict()
jQuery(document).ready(function() {
    $(".page-scroll").click(function() { /* ... */ });
});

// ou utilizar o jQuery como $ apenas neste arquivo e não globalmente
jQuery.noConflict()
(function($) {
    $(document).ready(function() {
        $(".page-scroll").click(function() { /* ... */ });
    });
}(jQuery));
  • Dude, you’re giving jquery.noConflict(): Uncaught Referenceerror: jQuery is not defined, knows why this error is happening? is working again but it is running straight to the page, I think using the native scroll of wordpress, I do not know if because of this error is not getting the smooth slide of the page in function

  • 1

    found the bug, was loading this script before the call of Jquery thank you very much for the help

Browser other questions tagged

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