faqts : Computers : Programming : Languages : JavaScript : Document

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

61 of 102 people (60%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How do I force user to save and download a .exe file without displaying a run option ?

Dec 13th, 2000 04:14
Juergen Thelen, David H,


As far as I know there's no javascript method to block the run option 
in the standard file save dialog (the one that comes up when you left 
click an URL whose HREF tag is armed with an .exe file name).
For IE4+ it is possible to skip the standard file save dialog by using 
something like document.execCommand('SaveAs', null, 'whatever.htm'), 
but this only works for HTM(L) and TXT files, and not for EXEs.
The only workaround that comes to my mind is to simply block left click 
functionality of the appropriate HREF while notifying the user, that he 
or she has to right click the link, and select 'Save Target/Link as...' 
instead.
For IE4+/NN4 this can be achieved very simple, as this snippets shows:
--- snip ---
<html>
<head>
<title>FAQTS - Juergen Thelen - Download .EXE w/o Open dialog (IE4+/NN4)
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<p><a onClick="alert('Please right click and use \'Save Target/Link 
as\'.'); return false" href="thefile.exe">thefile.exe</a></p>
<p><a onMouseOver="self.status='restricted info'; return true" 
onMouseOut="self.status=''" onClick="alert('Please right click and use 
\'Save Target/Link as\'.'); return false" 
href="thefile.exe">thefile.exe</a> 
</p>
</body>
</html>
--- snap ---
The first link shown by this example has all you need.
We need HREF here because the 'Save Target/Link as..." function of the 
context menu (popping up by right click) takes the URL to the .exe file 
to be downloaded from this HREF.
On the other hand we have to prevent that left clicking this HREF will 
pop up the standard open dialog, so we gonna use the higher priority of 
onClick vs HREF to first notify the user how to download and then 
cancel this left click by returning false.
The second link shown by this example additionally uses onMouseOver and 
onMouseOut to modify the message usually shown in the browser statusbar.
This one comes handy when you don't want the user to see the fully 
qualified URL to this HREF at the first blush...
Hth, Juergen