0

Decided to use echarts.js for a specific page in my site. Tried to download the package file on my server and then I tried to include it using a script tag. However, when I open the page I get a thousand different errors for it and not an exact cause for my error.

How can I include an external javascript file in my HTML and make it available or even run on load?

3
  • You need to provide more information for anybody to be able to help you. What are the errors you're seeing, how did you add the js? Commented Jul 11, 2022 at 4:48
  • Which Magento version? Magento 1 and Magento 2.x will have different answers. Also, please, post your errors. Commented Jul 11, 2022 at 5:53
  • I think its Magento 1. Since the site is migrating to magento 2 in a few weeks. Commented Jul 12, 2022 at 6:13

1 Answer 1

0

Option 1:

You can add js using layout as below:

By utilizing default_head_blocks.xml in app/design/frontend/Vendor/Theme/Magento_Theme/layout/

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <!-- Add local resources -->
        <css src="css/my-styles.css"/>

        <!-- The following two ways to add local JavaScript files are equal -->
        <script src="Magento_Catalog::js/sample1.js"/>
        <link src="js/sample.js"/>

        <!-- Add external resources -->
    <css src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css" src_type="url" />
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" src_type="url" />
        <link src="http://fonts.googleapis.com/css?family=Montserrat" src_type="url" /> 
    </head>
</page>

Option 2:

by utilizing requirejs-config.js in your themes or custom module

var config = {
map: {
    '*': {
          echarts : 'Magento_Theme/js/echarts.js'
     }
    }
};

You can include in phtml by utilizing below code

require([ 'jquery' , 'echarts'],function($){
    $(document).ready(function($){
        //custom js code goes here
    });
});
1
  • This is a Magento 1 specific answer, I'll ask the OP what Magento version he's using. Commented Jul 11, 2022 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.