I've created a CMS block with basic HTML, but I want to add my own custom Jquery.

I've been working on a timer project, I've done my own timer and the HTML and the styling are all prepared. But the only thing I need is a location for the custom Jquery.

I have no idea where this should go!

  • Hi! Welcome to Magento Stackexchange, do you want this Jquery to load on every page or just this specific page/block? – John Feb 13 '18 at 11:01
  • Also, I love the word nooblet and do you have a theme if you want this jquery to run on every page? – John Feb 13 '18 at 11:02
  • It will be located in the header so it's going to need to be every page. – S.Wilk Feb 13 '18 at 11:03
  • We've got a purchased theme. (So I'm editing their theme. A LOT) – S.Wilk Feb 13 '18 at 11:04
  • Alright, do you have a theme? Are you using require JS and/or have you already linked to a Jquery CDN? This is achievable by putting your jquery inside a javascript file and then telling Magento to load your file using default_head_blocks.xml – John Feb 13 '18 at 11:06

From my conversation in the comments with you You're going to need to go to paste your Jquery inside web/js and call it something like, lets say function.js. Save this inside that folder, next go to Vendor/Theme and if there's a Magento_Theme folder there go inside layout and click on default_head_blocks.xml. If not, you will need to create this file structure. (vendor/theme/magento_theme/layout/default_head_blocks.xml). Inside here you're going to want to insert <script src="js/function.js"/> line just after

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

if you're making this file you then want to write


after it. Save, flush cache and reload the page and the file should be referenced in your head. If it is but it isn't working, you can also use this to insert a Jquery cdn. I hope i've helped

  • Anytime! Welcome to the community! – John Feb 13 '18 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.