GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.36k stars 4.05k forks source link

Script tag not rendering in HTML page #3730

Closed hrdkzala closed 3 years ago

hrdkzala commented 3 years ago

I created the component as below.

const script = function() {
        const script = document.createElement('script');
        script.src = 'http://localhost:8888/builder/sites/site1/assets/js/jquery-1.12.4.min.js';
        document.body.appendChild(script);
});
editor.Components.addType('comp-with-js', {
      model: {
        defaults: {
          script
        }
      }
    });
editor.BlockManager.add('site_1', {
      label: 'Template 1',
      category: 'Template',
      content: '<div> HERE is my content </div>'
});

site_1 - is my readymade template and it has CSS and javascript.

CSS loads properly with the "content" but javascript doesn't so I created component for javascript.

On the HTML page, It shows the jquery file is added but doesn't work. I checked in view source.

If I made any external javascript file which simply alert("test"), but it doesn't work on html page.

I am using NuxtJS + Grapesjs.

Please advise me about this issue.

artf commented 3 years ago

Sorry but I don't understand where is your issue. You have created comp-with-js component but then you don't show where you are using it... Please, provide a reproducible demo.

mingxin-yang commented 3 years ago

script function don't work after drag the block @artf

mingxin-yang commented 3 years ago

https://grapesjs.com/docs/modules/Components-js.html#basic-scripts

hrdkzala commented 3 years ago

I have already followed it. but my question is that on the frontend where we display the page which is made by grapesjs editor, script/Js not loading.

My page is in NuxtJS.

mingxin-yang commented 3 years ago

I face same problem

mingxin-yang commented 3 years ago

https://grapesjs.com/docs/modules/Components-js.html#basic-scripts

this don't work for me

artf commented 3 years ago

@hrdkzala please create a proper bug issue with a reproducible demo, at the moment, I don't have enough information to help you, and from what I see, components with js are working properly.

ronaldohoch commented 2 years ago

Hello! I'm doing this right now and, by your code, you forgot to set the type of your block, check the way it work:

editor.BlockManager.add('site_1', {
      label: 'Template 1',
      category: 'Template',
      content: '<div> HERE is my content </div>',
      content: {
          type: 'comp-with-js'
      }
});