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);
}
}
}
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
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?
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.
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.
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.
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.
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"];
Sorry, that was an error on me posting. With quotes fixed, it still doesn't work.
That's me, nit-picker extraordinaire. Heh.
Sounds like you have something going, so I'll lurk until you say whether it worked.
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.
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.