Dashboards & Visualizations

Learning to include jquery in simple xml

maurelio79
Communicator

Hi to all, i'm just learning to usi a little of custom jquery in splunk simple dashboard.
I have this at begining of my xml:

<form script="script.js" stylesheet="dark.css">

in script.js i have this:

require([
     'jquery',
     'splunkjs/mvc/simplexml/ready!'
 ], function($) {
    $(document).ready(function() {
        $( "#row1" ).addClass( "red" );
    }); 
});

just a stupid code to see if it's works, but it does not.

Of cource in .css i have the class red. Even if i run in the chrome console:$( "#row1" ).addClass( "red" ); nothing happens.

Could you please help me?

Thanks and regards.

Tags (3)

BernardEAI
Communicator

I tried to get a function to run on page load, making use of the code you have shown:

$(document).ready(function() {
	console.log("page opened");
});

I just could not get that event to trigger on page load (I did a bump and refreshed the page). I finally tried:

$(function(){ console.log("page opened"); });

 and this worked. No idea why,  in my mind these two are equivalent.

0 Karma

maurelio79
Communicator

Thanks to all. It works, but it seems that i need a splunkweb restart every time i modify a css or js, is it normal?
Thanks and regards

0 Karma

justinatpnnl
Communicator

No, a restart isn't necessary every time. If you use the link @kurdbahr provided in the second part of his answer, it creates a new cache for JavaScript and CSS files and then you can just refresh the page.

http://your_splunk_server/en-US/_bump

renjujacob88
Path Finder
0 Karma

niketn
Legend

@renjujacob88 it is actually http://<splunkURLPort>/en-US/debug/refresh. For Development or Non-prod instances, _bump, debug refresh or Development mode settings are recommended.

Following is the URL to turn on Development Mode settings with no caching: https://docs.splunk.com/Documentation/Splunk/latest/AdvancedDev/CustomVizTutorial#Development_mode_s...

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

kurdbahr
Path Finder

As @justinatpnnl pointed out, your Javascript is probably not included in the web page because the Splunk server has not yet noticed your new Javascript file.

To make a new Javascript file available without a full server restart:

$SPLUNK_HOME/bin/splunk restartss

To force a reload of a changed Javascript file:

http(s)://localhost:8000/en-US/_bump

and click the button there to increment the version number.

justinatpnnl
Communicator

Your sample code does work, so my guess is that your script isn't being seen by Splunk and isn't loading. View the page source to see if Splunk was able to find it and load it into your dashboard.

The CSS should appear in the head tag:

<head>
    ...
    <link rel="stylesheet" type="text/css" media="all" href="/en-US/static/@264376:0/app/search/dark.css" />
    ...
</head>

The JavaScript should be at the bottom of the page just before the closing of the body tag:

    <script src="/en-US/static/@264376:0/app/search/script.js" type="text/javascript"></script>
</body>

Usually in order to get my new scripts and css files to populate in a dashboard I need to restart Splunk.

Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...