Categories
discuss

Detect from browser if specific application is installed

We have an advanced webpage (ASP.NET, C#), and a application which needs to be installed on the client computer in order to utilize the webpage to its fullest. The application is a tray app, and has primarily two tasks. Detect when certain events happen on the webserver (for instance invited to a meeting, or notify of an upcoming meeting). The other task the trayapp has is to use a custom protocol (trayapp://) to perform some ajax calls back to the server.

One problem we have is how to determine if the application is installed on the local machine or not. Now the user has to tick a checkbox to inform the website that the application is installed, and that it’s safe to call the trayapp:// url calls.

Is there any way, for instance through a JavaScript or similar to detect if our application is installed on the local machine?

The check needs to work for IE, FF and Opera browsers.

Answer

If you want to detect with javascript inside the browser, you can probably use the collection “navigator.plugins”. It works with Firefox, Opera and Chrome but unfortunately not with IE.

Update: In FF, Opera and Chrome you can test it easily like this:

if (navigator.plugins["Adobe Acrobat"]) {
// do some stuff if it is installed
} else {
// do some other stuff if its not installed
}

Update #2: If it is an ActiveX object in IE you can test if it exists by using something like this:

function getActiveXObject(name){
        try{
            return new ActiveXObject(name);
        }
        catch(err){
            return undefined;
        }
};

Another approach for IE is something similar to what JohnFx suggested (I found it here and have not tested it):

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurr entVersionInternet
SettingsUser AgentPost Platform
Categories
discuss

differences between 2 JUnit Assert classes

The JUnit framework contains 2 Assert classes (in different packages, obviously) and the methods on each appear to be very similar. Can anybody explain why this is?

The classes I’m referring to are: junit.framework.Assert and org.junit.Assert.

Answer

The old method (of JUnit 3) was to mark the test-classes by extending junit.framework.TestCase. That inherited junit.framework.Assert itself and your test class gained the ability to call the assert methods this way.

Since version 4 of JUnit, the framework uses Annotations for marking tests. So you no longer need to extend TestCase. But that means, the assert methods aren’t available. But you can make a static import of the new Assert class. That’s why all the assert methods in the new class are static methods. So you can import it this way:

import static org.junit.Assert.*;

After this static import, you can use this methods without prefix.

At the redesign they also moved to the new package org.junit that follows better the normal conventions for package naming.

Categories
discuss

Best way to find “next” form input element in jQuery?

Using jQuery, what’s the best way to find the next form element on the page, starting from an arbitrary element? When I say form element I mean <input>, <select>, <button> or <textarea>.

In the following examples, the element with the id “this” is the arbitrary starting point, and the element with the id “next” is the one I want to find. The same answer should work for all examples.

Example 1:

<ul>
 <li><input type="text" /></li>
 <li><input id="this" type="text" /></li>
</ul>

<ul>
 <li><input id="next" type="text" /></li>
</ul>

<button></button>

Example 2:

<ul>
 <li><input id="this" type="text" /></li>
</ul>

<button id="next"></button>

Example 3:

<input id="this" type="text" />
<input id="next" type="text" />

Example 4:

<div>
  <input id="this" type="text" />
  <input type="hidden" />
  <div>
    <table>
      <tr><td></td><td><input id="next" type="text" /></td></tr>
    </table>
  </div>
  <button></button>
</div>

EDIT: The two answers provided so far both require writing a sequence number to all input elements on the page. As I mentioned in the comments of one of them, this is kind of what I’m already doing and I would much prefer have a read-only solution since this will be happening inside a plugin.

Answer

kudos,

What about using .index?

e.g $(':input:eq(' + ($(':input').index(this) + 1) + ')');

Categories
discuss

when does a thread go out of scope?

I’ve written a program that counts lines, words, and characters in a text: it does this with threads. It works great sometimes, but not so great other times. What ends up happening is the variables pointing to the number of words and characters counted sometimes come up short and sometimes don’t.

It seems to me that the threads are sometimes ending before they can count all the words or characters that they want to. Is it because these threads go out of scope when the while (true) loop breaks?

I’ve included the code from the thready part of my problem below:

private void countText() {
  try {
    reader = new BufferedReader(new FileReader("this.txt"));
    while (true) {
      final String line = reader.readLine();
      if(line == null) {break;}
      lines++;
      new Thread(new Runnable() {public void run() {chars += characterCounter(line);}}).start();
      new Thread(new Runnable() {public void run() {words += wordCounter(line);}}).start();
      println(line);
    }

  } catch(IOException ex) {return;}

}

(Sub Question: This is the first time I’ve asked about something and posted code. I don’t want to use StackOverflow in place of google and wikipedia and am worried that this isn’t an appropriate question? I tried to make the question more general so that I’m not just asking for help with my code… but, is there another website where this kind of question might be more appropriate?)

Answer

A different threaded design would make it easier to find and fix this kind of problem, and be more efficient into the bargain. This is a longish response, but the summary is “if you’re doing threads in Java, check out java.util.concurrent as soon as humanly possible)”.

I guess you’re multithreading this code to learn threads rather than to speed up counting words, but that’s a very inefficient way to use threads. You’re creating two threads per line – two thousand threads for a thousand line file. Creating a thread (in modern JVMs) uses operating system resources and is generally fairly expensive. When two – let alone two thousand – threads have to access a shared resource (such as your chars and words counters), the resulting memory contention also hurts performance.

Making the counter variables synchronized as Chris Kimpton suggests or Atomic as WMR suggests will probably fix the code, but it will also make the effect of contention much worse. I’m pretty sure it will go slower than a single-threaded algorithm.

I suggest having just one long-lived thread which looks after chars, and one for words, each with a work queue to which you submit jobs each time you want to add a new number. This way only one thread is writing to each variable, and if you make changes to the design it’ll be more obvious who’s responsible for what. It’ll also be faster because there’s no memory contention and you’re not creating hundreds of threads in a tight loop.

It’s also important, once you’ve read all the lines in the file, to wait for all the threads to finish before you actually print out the values of the counters, otherwise you lose the updates from threads that haven’t finished yet. With your current design you’d have to build up a big list of threads you created, and run through it at the end checking that they’re all dead. With a queue-and-worker-thread design you can just tell each thread to drain its queue and then wait until it’s done.

Java (from 1.5 and up) makes this kind of design very easy to implement: check out java.util.concurrent.Executors.newSingleThreadExecutor. It also makes it easy to add more concurrency later on (assuming proper locking etc), as you can just switch to a thread pool rather than a single thread.

Categories
discuss

Ajax “Is there new content? If so, update page” – How to do this without breaking the server?

It’s a simple case of a javascript that continuously asks “are there yet?” Like a four year old on a car drive.. But, much like parents, if you do this too often or, with too many kids at once, the server will buckle under pressure..

How do you solve the issue of having a webpage that looks for new content in the order of every 5 seconds and that allows for a larger number of visitors?

Answer

stackoverflow does it some way, don’t know how though.

The more standard way would indeed be the javascript that looks for new content every few seconds.

A more advanced way would use a push-like technique, by using Comet techniques (long-polling and such). There’s a lot of interesting stuff under that link.

I’m still waiting for a good opportunity to use it myself…

Oh, and here’s a link from stackoverflow about it:
Is there some way to PUSH data from web server to browser?

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..