我正在使用一个 asp.net MVC4 Web 应用程序,如果客户端计算机尚未安装它,我需要从 ftp 安装一个软件。在安装期间将一些数据写入客户端计算机的注册表,例如:
Test
|__DefaultIcon
|__Shell
|__Open
|__command
这个入口只有在安装软件后才会出现。所以下次有人尝试下载它时我可以自动初始化软件,机器已经有软件了。使用以下代码初始化软件:
<script type="text/javascript">
function LaunchURLScript() {
var url = "Test:";
// Creates an object which can read files from the server
var reader = new XMLHttpRequest();
// Opens the file and specifies the method (get)
// Asynchronous is true
reader.open('GET', url, true);
//check each time the ready state changes
//to see if the object is ready
reader.onreadystatechange = checkReadyState;
function checkReadyState() {
if (reader.readyState === 4) {
//check to see whether request for the file failed or succeeded
if ((reader.status == 200) || (reader.status == 0)) {
//page exists -- redirect to the checked
//checked for page
// document.location.href = url;
window.open(url);
self.focus();
}
else {
alert("456");
//does nothing and quits the function
//if the url does not exist
return;
}
}//end of if (reader.readyState === 4)
}
// end of checkReadyState()
// Sends the request for the file data to the server
// Use null for "get" mode
try{
reader.send(null);
}
catch (err) {
alert(err.message);
}
}
</script>
但问题是即使url不存在window.open(url)也会执行,这意味着软件没有安装。请在调用window.open(url)之前帮助找出url是否存在