Showing results for 
Search instead for 
Did you mean: 

Adding javascript to cms page

Adding javascript to cms page


We are new to Magento2 and have a question with regards adding javascript to a CMS page.


We have followed the example provided here


We added this code

<script type="text/x-magento-init" xml="space">// <![CDATA[
{ "*": { "js/hello-world": {"a":1, "b":2}}}>
// ]]></script>

To the content of our home page.


When we refresh the page we get this error in the console

Uncaught SyntaxError: Unexpected token / in JSON at position 0


We didn't add the //'s and the CDATA to the code but it looks like they are causing the problem.


You can see this at


Any help would really be appreciated




Tags (1)

Re: Adding javascript to cms page

Do you really have hello-world.js file somewhere? 

Re: Adding javascript to cms page

Hi KarlaSa

Thanks for your reply

We do have the file hello-world.js in the js folder (see below)magentojs.png

As per the video in youtube - and the file contains the following

define (["jquery","underscore"],

function($, _) {

alert('hello to you sir')

return function(config, element) {




It appears that the error is caused by the code added to the cms home page


Thanks for your help