runApp() link works in IE, not in Chrome or Firefox
I have a lab management webapp that lists servers on my network. With a click in IE I can open a terminal services session to that server. The code works in IE, but not in chrome or firefox. Can anyone suggest how I can make this work in Chrome (Firefox would be nice too, but Chrome is more important.)
PHP Code:
<span style="color:blue;cursor:hand;" onClick="runApp('mstsc /v 10.1.2.3'); return false;" title="Click to open Terminal Services session">HOSTNAME</span>
I've had bad luck trying to insert strings of JavaScript into my HTML as well. I would suggest using a pattern called "unobtrusive" JavaScript where you instead attach the events to the elements in JS code and leave the HTML clean.
Interesting. So I'd create a script with a document.getElementById() line for each server in the list (there could be 100 in the list) then create the span with the unique id in the list for each server?
Okay, so firstly there are libraries like jQuery in which you can select multiple HTML elements and apply the same behaviors to them with minimal code.
With this selection we can then apply a click event to all elements with the class "server":
$(".server").click(function () {
runApp();
});
Now here comes the fun part. We nee to pass unique parameters for each element. There are many ways to extract information from an HTML element but the best is to use what's called a data attribute. So for example we can re-write our span tags like so:
Now we're selecting all elements with class "server", applying a click function to each of them, and calling a function runApp() which passes in the data-server attribute on each tag.
Download jquery and play around with a solution like this and I think you'll be happier than the solution where you have to mix JavaScript and HTML code together. Much more readable.
Bookmarks