Dashboards & Visualizations

Why is my javascript function not executing in my dashboard?

tmontney
Builder

Branching off my last question, I now cannot get this function to load on button click. Works fine in a local html file.

<dashboard>
  <label>test</label>
  <row>
    <panel>
      <html>
               <div id="frm" style="width:  300px; height: 100px; float: left;">
                 <form script="users.js">
         Usernames (separate by comma): <input type="text" id="uname"/>
         <input id="unameSubmit" type="button" value="Submit" onclick="httpPost('http://www.example.com')"/>
         </form>
         </div>
       <div id="main" style="width: 400px; height: 300px; float:left;"></div>
           </html>
    </panel>
  </row>
</dashboard>

This time I separated my .js file.

         function httpPost(theUrl)
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4)
        {
        if (xmlhttp.status==200)
        {
            writetobody(xmlhttp.responseText);
            }
        }
    }
    var unameVal = document.getElementById("uname").value;
    var newUrl = theUrl.replace("REPLACEME", unameVal);
    xmlhttp.open("GET", newUrl, true );
    xmlhttp.send(); 
}
            function writetobody(data) {
            if (data!=null) {
            var json = JSON.parse(data);
            for (i = 0; json.length > i; i++) {
            var val1 = json[i]["val1"];
            var val2 = json[i]["val2"];
                        document.getElementById('main').innerText=("\r\nval1: " + val1 + "\r\nval2" + val2);
            }

            }
            }
0 Karma

bmacias84
Champion

What your trying to do is use Simple XML extensions and my understanding Splunk's Simple XML html does not support script tags. For scripts to work you must embed your script in the parent/root tag which is dashboard or form. If you are using HTML dashboards you can use the script tag using relative path of your script.

example

<form script="users.js">
OR 
<dashboard script="users.js">

The script must be within the same app context as the view/ dashboard. For more information please read Modify dashboards using Simple XML extensions

tmontney
Builder

Makes sense. Do I have to restart the Splunk server to reflect changes if I move/modify the .js file? Would inline JS be better?

0 Karma

bmacias84
Champion

If you move the js file, yes. Modifying technically no, but if this is production you probably have js caching turning on in your web/server config. You can use the bump url and/ or debug URL to clear force Splunk to recognize the modified script. I have other post scattered about on answer about those topics.

0 Karma

tmontney
Builder

I can confirm now the dashboard sees the script. When hitting edit, it references a custom script. Monitoring network traffic (via browser debug tools), I can see a "200 OK" for my .js file. I can also even browse to the path. Still, the function is not being executed.

0 Karma

tmontney
Builder

Moved the .js file to the same folder as the .xml dashboard file. Tried both form script and dashboard script to no avail. Even restarted the splunk server.

I also created a new dashboard for just this example.

0 Karma

bmacias84
Champion

Please read Modify dashboards using Simple XML extensions
which will answer your question on why the way you are doing it does not work. File have to placed in specific directories.

0 Karma

DalJeanis
Legend

well, if you look at what you posted and notice the green text, it indicates what is in "quotes" of some kind.

Looks like the .JS file has a problem in this line -

 var val1 = json[i]["val1];
 var val2 = json[i]["val2"];

Which probably should read

 var val1 = json[i]["val1"];
 var val2 = json[i]["val2"];

tmontney
Builder

Sorry, that was an error on me posting. With quotes fixed, it still doesn't work.

0 Karma

DalJeanis
Legend

That's me, nit-picker extraordinaire. Heh.

Sounds like you have something going, so I'll lurk until you say whether it worked.

0 Karma

tmontney
Builder

Lol, you wouldn't know though. I should have double checked my post. As of right now, nothing's worked so far. Oddly enough when I was first trying this, it was working. But now I have no clue what's changed.

0 Karma

tmontney
Builder

Even still, it would send a GET request first. I have a breakpoint set on the server side and I see no traffic. That and Chrome doesn't show any HTTP requests.

0 Karma
Get Updates on the Splunk Community!

New Case Study Shows the Value of Partnering with Splunk Academic Alliance

The University of Nevada, Las Vegas (UNLV) is another premier research institution helping to shape the next ...

How to Monitor Google Kubernetes Engine (GKE)

We’ve looked at how to integrate Kubernetes environments with Splunk Observability Cloud, but what about ...

Index This | How can you make 45 using only 4?

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