Custom javascript not working
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
add a comment |
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
Given answer, please try it and let me if it works or not
– Prathap Gunasekaran
3 mins ago
add a comment |
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
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
magento2 javascript requirejs
asked 12 mins ago
JohnJohn
1063
1063
Given answer, please try it and let me if it works or not
– Prathap Gunasekaran
3 mins ago
add a comment |
Given answer, please try it and let me if it works or not
– Prathap Gunasekaran
3 mins ago
Given answer, please try it and let me if it works or not
– Prathap Gunasekaran
3 mins ago
Given answer, please try it and let me if it works or not
– Prathap Gunasekaran
3 mins ago
add a comment |
1 Answer
1
active
oldest
votes
Add an external JS in the following way :
Try this,
Add your custom js in the following path.
app/design/frontend/Vendor/theme-name/web/js/nameofjs.js
then you need to add requirejs-config.js to the following path.
app/design/frontend/Vendor/theme-name/requirejs-config.js
then add the following code in it
var config = {
paths: {
'your_js_name' : 'js/nameofjs',
},
shim: {
'your_js_name': {
deps: ['jquery']
},
}
};
and add the below code to load custom js, In your case it would be header.phtml in Magento_Theme folder in your theme
app/design/frontend/Vendor/theme-name/Magento_Theme/templates/html/header.phtml
require(['jquery','your_js_name'],function($) {
$(window).load(function () {
/*alert('load from external jquery');*/
});});
Hope this helps and this is correct way to load any custom js in M2 as far as I know.
Peace :)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add an external JS in the following way :
Try this,
Add your custom js in the following path.
app/design/frontend/Vendor/theme-name/web/js/nameofjs.js
then you need to add requirejs-config.js to the following path.
app/design/frontend/Vendor/theme-name/requirejs-config.js
then add the following code in it
var config = {
paths: {
'your_js_name' : 'js/nameofjs',
},
shim: {
'your_js_name': {
deps: ['jquery']
},
}
};
and add the below code to load custom js, In your case it would be header.phtml in Magento_Theme folder in your theme
app/design/frontend/Vendor/theme-name/Magento_Theme/templates/html/header.phtml
require(['jquery','your_js_name'],function($) {
$(window).load(function () {
/*alert('load from external jquery');*/
});});
Hope this helps and this is correct way to load any custom js in M2 as far as I know.
Peace :)
add a comment |
Add an external JS in the following way :
Try this,
Add your custom js in the following path.
app/design/frontend/Vendor/theme-name/web/js/nameofjs.js
then you need to add requirejs-config.js to the following path.
app/design/frontend/Vendor/theme-name/requirejs-config.js
then add the following code in it
var config = {
paths: {
'your_js_name' : 'js/nameofjs',
},
shim: {
'your_js_name': {
deps: ['jquery']
},
}
};
and add the below code to load custom js, In your case it would be header.phtml in Magento_Theme folder in your theme
app/design/frontend/Vendor/theme-name/Magento_Theme/templates/html/header.phtml
require(['jquery','your_js_name'],function($) {
$(window).load(function () {
/*alert('load from external jquery');*/
});});
Hope this helps and this is correct way to load any custom js in M2 as far as I know.
Peace :)
add a comment |
Add an external JS in the following way :
Try this,
Add your custom js in the following path.
app/design/frontend/Vendor/theme-name/web/js/nameofjs.js
then you need to add requirejs-config.js to the following path.
app/design/frontend/Vendor/theme-name/requirejs-config.js
then add the following code in it
var config = {
paths: {
'your_js_name' : 'js/nameofjs',
},
shim: {
'your_js_name': {
deps: ['jquery']
},
}
};
and add the below code to load custom js, In your case it would be header.phtml in Magento_Theme folder in your theme
app/design/frontend/Vendor/theme-name/Magento_Theme/templates/html/header.phtml
require(['jquery','your_js_name'],function($) {
$(window).load(function () {
/*alert('load from external jquery');*/
});});
Hope this helps and this is correct way to load any custom js in M2 as far as I know.
Peace :)
Add an external JS in the following way :
Try this,
Add your custom js in the following path.
app/design/frontend/Vendor/theme-name/web/js/nameofjs.js
then you need to add requirejs-config.js to the following path.
app/design/frontend/Vendor/theme-name/requirejs-config.js
then add the following code in it
var config = {
paths: {
'your_js_name' : 'js/nameofjs',
},
shim: {
'your_js_name': {
deps: ['jquery']
},
}
};
and add the below code to load custom js, In your case it would be header.phtml in Magento_Theme folder in your theme
app/design/frontend/Vendor/theme-name/Magento_Theme/templates/html/header.phtml
require(['jquery','your_js_name'],function($) {
$(window).load(function () {
/*alert('load from external jquery');*/
});});
Hope this helps and this is correct way to load any custom js in M2 as far as I know.
Peace :)
answered 8 mins ago
Prathap GunasekaranPrathap Gunasekaran
982313
982313
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Given answer, please try it and let me if it works or not
– Prathap Gunasekaran
3 mins ago