www.webdeveloper.com
Results 1 to 5 of 5

Thread: runApp() link works in IE, not in Chrome or Firefox

  1. #1
    Join Date
    May 2005
    Posts
    212

    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

  2. #2
    Join Date
    May 2012
    Posts
    4
    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.

    Ex:

    document.getElementById("mySpan").onclick = function() { runApp(); };

    <span id="mySpan"></span>

  3. #3
    Join Date
    May 2005
    Posts
    212
    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?

  4. #4
    Join Date
    May 2012
    Posts
    4
    Ah, so now we're getting a bit more complicated!

    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.

    So for example, the following elements:

    <span class="server">Server 1</span>
    <span class="server">Server 2</span>
    <span class="server">Server 3</span>

    Can be selected as such:

    $(".server")

    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:

    <span class="server" data-server="123">Server 1</span>
    <span class="server" data-server="456">Server 2</span>
    <span class="server" data-server="789">Server 3</span>

    And modify our click handler as follows:

    $(".server").click(function () {
    runApp($(this).data("server"));
    });

    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.

  5. #5
    Join Date
    May 2005
    Posts
    212
    Excellent. Thanks, I'll play with that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles