Custom javascript not working

The past tense for the quoting particle って

Why doesn't "adolescent" take any articles in "listen to adolescent agonising"?

Rationale to prefer local variables over instance variables?

How spaceships determine each other's mass in space?

Did Amazon pay $0 in taxes last year?

Are Wave equations equivalent to Maxwell equations in free space?

Learning to quickly identify valid fingering for piano?

The Key to the Door

Align equations with text before one of them

How can I be pwned if I'm not registered on the compromised site?

The need of reserving one's ability in job interviews

When to use the term transposed instead of modulation?

A bug in Excel? Conditional formatting for marking duplicates also highlights unique value

If nine coins are tossed, what is the probability that the number of heads is even?

What is "desert glass" and what does it do to the PCs?

Dukha vs legitimate need

Professor forcing me to attend a conference

Should I use HTTPS on a domain that will only be used for redirection?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

What is a term for a function that when called repeatedly, has the same effect as calling once?

Why can't we use freedom of speech and expression to incite people to rebel against government in India?

What is the purpose of a disclaimer like "this is not legal advice"?

Convert an array of objects to array of the objects' values

Has a sovereign Communist government ever run, and conceded loss, on a fair election?



Custom javascript not working














0















I'm trying to inject a scroll to anchor javascript globally in Magento 2.



More specifically this script:



$('a[href^="#"]').on('click', function(event) {
var target = $(this.getAttribute('href'));
if( target.length ) {
event.preventDefault();
$('html, body').stop().animate({
scrollTop: target.offset().top
}, 1000);
}
});


Taken from this blog post.



I converted it to include defines and stuff:



define([
"jquery",
"jquery/ui"
], function ($) {
'use strict';

$('a[href^="#"]').on('click', function(event) {
var target = $(this.getAttribute('href'));
if( target.length ) {
event.preventDefault();
$('html, body').stop().animate({
scrollTop: target.offset().top
}, 1000);
}
});

});


And injecting it in my themes default_head_blocks.xml with <link src="js/custom.js" />



The injection works but I get the following error in Chrome console:



Uncaught Error: Mismatched anonymous define() module: function ($) {
'use strict';

require.js:166


What is wrong?









share



























    0















    I'm trying to inject a scroll to anchor javascript globally in Magento 2.



    More specifically this script:



    $('a[href^="#"]').on('click', function(event) {
    var target = $(this.getAttribute('href'));
    if( target.length ) {
    event.preventDefault();
    $('html, body').stop().animate({
    scrollTop: target.offset().top
    }, 1000);
    }
    });


    Taken from this blog post.



    I converted it to include defines and stuff:



    define([
    "jquery",
    "jquery/ui"
    ], function ($) {
    'use strict';

    $('a[href^="#"]').on('click', function(event) {
    var target = $(this.getAttribute('href'));
    if( target.length ) {
    event.preventDefault();
    $('html, body').stop().animate({
    scrollTop: target.offset().top
    }, 1000);
    }
    });

    });


    And injecting it in my themes default_head_blocks.xml with <link src="js/custom.js" />



    The injection works but I get the following error in Chrome console:



    Uncaught Error: Mismatched anonymous define() module: function ($) {
    'use strict';

    require.js:166


    What is wrong?









    share

























      0












      0








      0








      I'm trying to inject a scroll to anchor javascript globally in Magento 2.



      More specifically this script:



      $('a[href^="#"]').on('click', function(event) {
      var target = $(this.getAttribute('href'));
      if( target.length ) {
      event.preventDefault();
      $('html, body').stop().animate({
      scrollTop: target.offset().top
      }, 1000);
      }
      });


      Taken from this blog post.



      I converted it to include defines and stuff:



      define([
      "jquery",
      "jquery/ui"
      ], function ($) {
      'use strict';

      $('a[href^="#"]').on('click', function(event) {
      var target = $(this.getAttribute('href'));
      if( target.length ) {
      event.preventDefault();
      $('html, body').stop().animate({
      scrollTop: target.offset().top
      }, 1000);
      }
      });

      });


      And injecting it in my themes default_head_blocks.xml with <link src="js/custom.js" />



      The injection works but I get the following error in Chrome console:



      Uncaught Error: Mismatched anonymous define() module: function ($) {
      'use strict';

      require.js:166


      What is wrong?









      share














      I'm trying to inject a scroll to anchor javascript globally in Magento 2.



      More specifically this script:



      $('a[href^="#"]').on('click', function(event) {
      var target = $(this.getAttribute('href'));
      if( target.length ) {
      event.preventDefault();
      $('html, body').stop().animate({
      scrollTop: target.offset().top
      }, 1000);
      }
      });


      Taken from this blog post.



      I converted it to include defines and stuff:



      define([
      "jquery",
      "jquery/ui"
      ], function ($) {
      'use strict';

      $('a[href^="#"]').on('click', function(event) {
      var target = $(this.getAttribute('href'));
      if( target.length ) {
      event.preventDefault();
      $('html, body').stop().animate({
      scrollTop: target.offset().top
      }, 1000);
      }
      });

      });


      And injecting it in my themes default_head_blocks.xml with <link src="js/custom.js" />



      The injection works but I get the following error in Chrome console:



      Uncaught Error: Mismatched anonymous define() module: function ($) {
      'use strict';

      require.js:166


      What is wrong?







      magento2 javascript requirejs





      share












      share










      share



      share










      asked 1 min ago









      JohnJohn

      1063




      1063






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f264940%2fcustom-javascript-not-working%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f264940%2fcustom-javascript-not-working%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

          How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

          變成蝙蝠會怎樣? 參考資料 外部連結 导航菜单Thomas Nagel, "What is it like to be a...