Dashboards & Visualizations

Adding javascript to splunk dashboard

nisha_kapoor
Path Finder

I am pretty new to Splunk, so this might seem a little basic, but can someone please share a demo javascript file? This is my simple test javascript code which is doesn't work.

function msg(){  
 alert("Hello Javatpoint");  
}  

Are there any require statements, etc which I am missing? I have pasted this file myScript.js in C:\Program Files\Splunk\etc\apps\search\appserver\static. This is how I reference it in Splunk:

<dashboard script="myScript.js">
  <label>Hello</label>
  <row>
     <panel>
    <html>
    <input type="button" value="click" onclick="msg()"/>  
  </html>
  </panel>
  </row>


</dashboard>

Also I restarted Splunk. But still the message is not displayed. Is there something I am missing?

Tags (1)
0 Karma

niketn
Legend

@nisha_kapoor , I have already answered this query in my answer. Please refer to the same. There is an example of JavaScript alert and another one for Modal Error Message.

https://answers.splunk.com/answers/558642/access-a-drop-down-field-value-in-javascript.html

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

hardikJsheth
Motivator

Here is the updated JS

require(['jquery','underscore','splunkjs/mvc/searchmanager','splunkjs/mvc','splunkjs/mvc/utils','splunkjs/mvc/simplexml/ready!','splunkjs/ready!'], function($,_,SearchManager,mvc){
console.log("HI");
        var submitted = mvc.Components.getInstance('submitted', {create: true});
        console.log(submitted)
        function msg(){
                 console.log("Hi I am here");
                 alert("Hello Javatpoint");
         }
        submitted.on('click:btn',msg);
        msg();
});

You need to set id=btn for your button.

0 Karma
Get Updates on the Splunk Community!

From Alert to Resolution: How Splunk Observability Helps SREs Navigate Critical ...

It's 3:17 AM, and your phone buzzes with an urgent alert. Wire transfer processing times have spiked, and ...

ATTENTION!! We’re MOVING (not really)

Hey, all! In an effort to keep this Slack workspace secure and also to make our new members' experience easy, ...

Splunk Admins: Build a Smarter Stack with These Must-See .conf25 Sessions

  Whether you're running a complex Splunk deployment or just getting your bearings as a new admin, .conf25 ...