<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Splunk Javascript - Calling a function in a JS from another JS in Dashboards &amp; Visualizations</title>
    <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-Javascript-How-to-call-a-function-in-a-JS-from-another-JS/m-p/593792#M48687</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;thanks for your example, but I am facing some difficulties in importing a little complex js file that expose some functions.&lt;/P&gt;&lt;P&gt;The complete code is &lt;A href="https://unpkg.com/cronstrue@1.61.0/dist/cronstrue-i18n.min.js" target="_blank" rel="noopener"&gt;here&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This is the beginning of the js file:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;!function(t, e) {
    "object" == typeof exports &amp;amp;&amp;amp; "object" == typeof module ? module.exports = e() : "function" == typeof define &amp;amp;&amp;amp; define.amd ? define("cronstrue", [], e) : "object" == typeof exports ? exports.cronstrue = e() : t.cronstrue = e()
}(globalThis, (function() {
    return (()=&amp;gt;{
        "use strict";
        var t = {
            794: (t,e,n)=&amp;gt;{
                Object.defineProperty(e, "__esModule", {
                    value: !0
                }),
                e.CronParser = void 0
[...]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The end of the code is this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;         , e = {};
        function n(r) {
            var o = e[r];
            if (void 0 !== o)
                return o.exports;
            var i = e[r] = {
                exports: {}
            };
            return t[r](i, i.exports, n),
            i.exports
        }
        var r = {};
        return (()=&amp;gt;{
            var t = r;
            Object.defineProperty(t, "__esModule", {
                value: !0
            }),
            t.toString = void 0;
            var e = n(728)
              , o = n(336);
            e.ExpressionDescriptor.initialize(new o.enLocaleLoader),
            t.default = e.ExpressionDescriptor;
            var i = e.ExpressionDescriptor.toString;
            t.toString = i
        }
        )(),
      r
    );
  })();
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I import this, please?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot in advance.&lt;/P&gt;&lt;P&gt;Luca Caldiero&lt;/P&gt;</description>
    <pubDate>Thu, 14 Apr 2022 14:36:22 GMT</pubDate>
    <dc:creator>lucacaldiero</dc:creator>
    <dc:date>2022-04-14T14:36:22Z</dc:date>
    <item>
      <title>Splunk Javascript - How to call a function in a JS from another JS?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-Javascript-How-to-call-a-function-in-a-JS-from-another-JS/m-p/310140#M19806</link>
      <description>&lt;P&gt;Hello splunkers!&lt;/P&gt;
&lt;P&gt;I'm still learning how javascript works and how it works with splunk and I'm trying to do something simple, calling a function from one JS, like a library, but I don´t know what I'm doing wrong.&lt;BR /&gt;In this case I have a javascript called myLib.js which is this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;function doAlert(){
       alert("It works!");
}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I have another javascript called myJS.js which is like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;require(["./myLib.js",
        "splunkjs/mvc",
        "splunkjs/mvc/searchmanager",
        "splunkjs/mvc/simplexml/ready!"
    ], function (mvc) {

    /* Stuff */

    $(document).ready(function () {
        console.log("Document Ready!");
        /* More stuff */

        $("#myButton").on("click", doAlert);
        console.log("Document Ready Done!");
    });

});
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;On my xml I also have a button myButton but the problem is that I'm getting this messages on console.&lt;BR /&gt;&lt;EM&gt;&lt;A href="http://requirejs.org/docs/errors.html#scripterror" target="_blank"&gt;http://requirejs.org/docs/errors.html#scripterror&lt;/A&gt;&lt;BR /&gt;at makeError (eval at module.exports (common.js:248), :166:17)&lt;BR /&gt;at HTMLScriptElement.onScriptError (eval at module.exports (common.js:248), :1689:36)&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;I checked requiredjs error info but the only thing it says it's that I have an error on my script but it's only a function, I don't see any problem.&lt;/P&gt;
&lt;P&gt;I tried to put myLib.js in script=" " tag, writting it without .js, making a var myLib = require('myLib'); but always it's the same error.&lt;BR /&gt;It must be something really simple but I dont see it.&lt;BR /&gt;Please, can you help me with this problem?&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2022 17:01:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-Javascript-How-to-call-a-function-in-a-JS-from-another-JS/m-p/310140#M19806</guid>
      <dc:creator>garenilla</dc:creator>
      <dc:date>2022-04-14T17:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk Javascript - Calling a function in a JS from another JS</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-Javascript-How-to-call-a-function-in-a-JS-from-another-JS/m-p/310141#M19807</link>
      <description>&lt;P&gt;I think the docs here will do a good job of walking you through what you're trying to do:&lt;BR /&gt;
&lt;A href="http://dev.splunk.com/view/webframework-developapps/SP-CAAAESY"&gt;http://dev.splunk.com/view/webframework-developapps/SP-CAAAESY&lt;/A&gt;&lt;BR /&gt;
This is also a really good previous answer that walks through how to integrate with &lt;CODE&gt;require&lt;/CODE&gt;:&lt;BR /&gt;
&lt;A href="https://answers.splunk.com/answers/482069/how-to-include-js-file-inside-another-js-file.html"&gt;https://answers.splunk.com/answers/482069/how-to-include-js-file-inside-another-js-file.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Start by looking at how to integrate your code in here:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; require(["./myLib.js",
         "splunkjs/mvc",
         "splunkjs/mvc/searchmanager",
         "splunkjs/mvc/simplexml/ready!"
     ], function (mvc) {...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I believe the only library you can import in require without adding the name into the function argument list is &lt;CODE&gt;splunkjs/mvc/simplexml/ready!&lt;/CODE&gt;. So that snippet above should probably be more like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; require(["./myLib.js",
         "splunkjs/mvc",
         "splunkjs/mvc/searchmanager",
         "splunkjs/mvc/simplexml/ready!"
     ], function (myLib, mvc, SearchManager) {...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and then when you want to reference the &lt;CODE&gt;doAlert&lt;/CODE&gt; function, I believe you'll need to reference it within the myLib namespace: &lt;CODE&gt;myLib.doAlert()&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;But the guides will be a great place to help you understand the nitty gritty and save you a lot of pain as you develop.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 17:33:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-Javascript-How-to-call-a-function-in-a-JS-from-another-JS/m-p/310141#M19807</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-02-23T17:33:04Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk Javascript - Calling a function in a JS from another JS</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-Javascript-How-to-call-a-function-in-a-JS-from-another-JS/m-p/310142#M19808</link>
      <description>&lt;P&gt;Hi eliot,&lt;/P&gt;

&lt;P&gt;thank you for the quick reply, it helped me a lot! Now I understand more about loading libraries and also I have cleaned my code.&lt;BR /&gt;
I have been digging a little and I have found what it was wrong on my code.&lt;/P&gt;

&lt;P&gt;First I needed to add a  define([]) clause on myLib and return it. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    define(["splunkjs/mvc/simplexml/ready!"], function () {
            return myLib = (function () {
                var myLib  = {
                    doAlert: function(){
                        console.log("It Works!");
                    }
                }
                return myLib;
            })();
        });
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then on main.js I specified the route for the lib on the app &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    require(["../app/myApp/myLib",
            "splunkjs/mvc",
            "splunkjs/mvc/simplexml/ready!"
        ], function (myLib,mvc) {
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Finally on a more deep digging, I found where are all the js splunk use, like mvc. There are on /share/splunk/search_mrsparkle/exposed/js &lt;/P&gt;

&lt;P&gt;Thank you for all your help!&lt;BR /&gt;
Best regards!&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 10:48:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-Javascript-How-to-call-a-function-in-a-JS-from-another-JS/m-p/310142#M19808</guid>
      <dc:creator>garenilla</dc:creator>
      <dc:date>2018-02-26T10:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk Javascript - Calling a function in a JS from another JS</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-Javascript-How-to-call-a-function-in-a-JS-from-another-JS/m-p/593792#M48687</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;thanks for your example, but I am facing some difficulties in importing a little complex js file that expose some functions.&lt;/P&gt;&lt;P&gt;The complete code is &lt;A href="https://unpkg.com/cronstrue@1.61.0/dist/cronstrue-i18n.min.js" target="_blank" rel="noopener"&gt;here&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This is the beginning of the js file:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;!function(t, e) {
    "object" == typeof exports &amp;amp;&amp;amp; "object" == typeof module ? module.exports = e() : "function" == typeof define &amp;amp;&amp;amp; define.amd ? define("cronstrue", [], e) : "object" == typeof exports ? exports.cronstrue = e() : t.cronstrue = e()
}(globalThis, (function() {
    return (()=&amp;gt;{
        "use strict";
        var t = {
            794: (t,e,n)=&amp;gt;{
                Object.defineProperty(e, "__esModule", {
                    value: !0
                }),
                e.CronParser = void 0
[...]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The end of the code is this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;         , e = {};
        function n(r) {
            var o = e[r];
            if (void 0 !== o)
                return o.exports;
            var i = e[r] = {
                exports: {}
            };
            return t[r](i, i.exports, n),
            i.exports
        }
        var r = {};
        return (()=&amp;gt;{
            var t = r;
            Object.defineProperty(t, "__esModule", {
                value: !0
            }),
            t.toString = void 0;
            var e = n(728)
              , o = n(336);
            e.ExpressionDescriptor.initialize(new o.enLocaleLoader),
            t.default = e.ExpressionDescriptor;
            var i = e.ExpressionDescriptor.toString;
            t.toString = i
        }
        )(),
      r
    );
  })();
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I import this, please?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot in advance.&lt;/P&gt;&lt;P&gt;Luca Caldiero&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2022 14:36:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-Javascript-How-to-call-a-function-in-a-JS-from-another-JS/m-p/593792#M48687</guid>
      <dc:creator>lucacaldiero</dc:creator>
      <dc:date>2022-04-14T14:36:22Z</dc:date>
    </item>
  </channel>
</rss>

