Sociable

Wednesday, October 7, 2009

Launching a ClickOnce Application using JavaScript

We wrote a web application which needs to launch a Microsoft ClickOnce application, while at the same time redirect the user to a new page.  The basic flow of the program is this:

  1. User navigates to web application
  2. Unique GUID for transaction ID is generated for the ClickOnce application to use
  3. The user clicks the launch link and the GUID is passed to the ClickOnce application as a URL parameter
    1. a) the ClickOnce app launches
    2. b) the user is taken to a “wait for the results screen” while the ClickOnce application is working
  4. The ClickOnce application takes a measurement with a USB device
  5. The ClickOnce application calls a web service to store the measurement, using the passed-in GUID as the key
  6. The ClickOnce application closes itself
  7. The “wait for results screen” meta-refreshes until it sees the results in the underlying database
  8. The “wait for results screen” redirects the user back to the measurements list page

The problem we were facing had to do with using a JavaScript window.open() function to launch the “.application” ClickOnce URL.  Basically, when we did this it opened a new browser window, which immediately closed without launching the ClickOnce application.

Solution:

Use the JavaScript setTimeout() function.

<html>
<head>

<script type="text/javascript">
function LaunchApp() {
setTimeout("location.replace('waitPage.htm')", 2000);
}

</head>

<body>
<!-- the id below is generated by the code behind -->
<a onclick="javascript:LaunchApp();"
href="MyClickOnceApp.application?id=30F0D180-2CFC-3A80-F90922B0B31B656A">
Click here to launch application</a>
</body>
</html>

0 comments: