Dashboards & Visualizations

Custom Javascript throw exception: "Splunk is not defined"

barakakha
New Member

I'm trying to add custom Javascript to my simple XML dashboard (as explained here) but when it start running it throw an exception "Splunk is not defined".

This is my js code with some modification to find why its not running:

try{
    if( Splunk.Module.SimpleResultsTable ){
    Splunk.Module.SimpleResultsTable = $.klass(Splunk.Module.SimpleResultsTable, {
        renderResults: function($super, htmlFragment) {
            $super(htmlFragment);

            if (this.getInferredEntityName()=="events") {
                this.renderedCount = $("tr", this.container).length - 1;
            }

            $('#highlight td').each(function() {
                $(this).attr("data-value",$(this).html().trim());
            });
        }
    });
 }
}
catch(err){
    alert(err.message);
}

what am i doing wrong?

Tags (2)
0 Karma
1 Solution

jeffland
SplunkTrust
SplunkTrust

Your javascript file needs to load some dependencies. Right now, you are telling the browser to out of the blue use an object called "Splunk", but you didn't tell it where to find that object. Have a look here at the third gray box to see that your .js needs to have roughly the following format:

require([
"splunkjs/ready!",
"splunkjs/mvc/chartview"
], function(mvc) {
    var Chart = require("splunkjs/mvc/chartview"); // otherwise "Chart" would be undefined etc.
    // further js goes here
});

The splunkjs/ready! is needed to use Splunk in your js code I believe, so placing the code you currently have where the comment indicates should do the trick for you, at least as far as the error you currently have is concerned.

View solution in original post

0 Karma

jeffland
SplunkTrust
SplunkTrust

Your javascript file needs to load some dependencies. Right now, you are telling the browser to out of the blue use an object called "Splunk", but you didn't tell it where to find that object. Have a look here at the third gray box to see that your .js needs to have roughly the following format:

require([
"splunkjs/ready!",
"splunkjs/mvc/chartview"
], function(mvc) {
    var Chart = require("splunkjs/mvc/chartview"); // otherwise "Chart" would be undefined etc.
    // further js goes here
});

The splunkjs/ready! is needed to use Splunk in your js code I believe, so placing the code you currently have where the comment indicates should do the trick for you, at least as far as the error you currently have is concerned.

0 Karma

barakakha
New Member

Thank you!! that was it..

0 Karma

acharlieh
Influencer

As explained where? Did you intend to include a link?

0 Karma

barakakha
New Member
0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...