Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invoking a download using JavaScript

Status
Not open for further replies.

rkmarcks

Instructor
May 28, 2000
43
US
I have a situation where I give the user a choice to view a pop-up window, to view one of two pdf files or two download one of two rtf files.  The selection is done by checking an appropriate radio button.  I use a javascript URL in the onclick option of an HTML <a href....> tag.  The view pop-up or view pdf works great.  However, when download rtf is selected, the browser will start the download, but also opens a new [blank] window.  In IE, this is the can't find URL screen.  How do I have JavaScript request a particular file for download without opening up a blank window in the process?
 
Dear rkmarcks,<br><br>Could I see the entire 'anchor' tag you are referring to?<br><br>-pete
 
What I've found out is that I can download a file and open it using an application.&nbsp;&nbsp;The window that pop's up must be used by the application ie: MS Word for an RTF.&nbsp;&nbsp;But when I save to file, it will in fact save, but I have an extraneous window.&nbsp;&nbsp;I did read someplace the window may need to pop up, but it could be a hidden window.&nbsp;&nbsp;This would be ok since I could track the hidden window and close it without the user ever seeing it.&nbsp;&nbsp;But I don't know how to do a hidden window either.<br><br>Here's what I have...<br><br>&lt;html&gt;<br>&lt;head&gt;<br>.<br>.<br>.<br>&lt;/head&gt;<br>&lt;body&gt;<br>.<br>.<br>.<br>&lt;script language=&quot;JavaScript&quot;&gt;<br>&lt;!--<br>var path;<br>var ext;<br>var win1;<br>var title = &quot;Course Description&quot;;<br>var detail = &quot;width=515,height=315,resize=no,scrollbars=no&quot;;<br><br>function dfn(pt,ttl,dtl)<br>{if(ext == &quot;.rtf&quot; ¦¦ ext == &quot;c.rtf&quot;)<br>&nbsp;&nbsp;&nbsp;{open(pt);&nbsp;&nbsp;//This is the line I'm having problems with.<br>&nbsp;&nbsp;&nbsp;&nbsp;win2 = pt.open;}<br>&nbsp;else<br>&nbsp;&nbsp;&nbsp;{win1=window.open(pt,ttl,dtl);}<br>}<br><br>//This function helps derive the variable with which I<br>// eventually concatenate with in the javascript URL below <br>//to form the URL from where I want to download.<br><br>function dothis()<br>{<br>&nbsp;{if (document.forms[0].elements.dowhat[0].checked == true)<br>&nbsp;&nbsp;&nbsp;&nbsp;{path=&quot;coursedesc/&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ext=&quot;.htm&quot;;}<br>&nbsp;&nbsp;else if (document.forms[0].elements.dowhat[1].checked == true)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{path=&quot;mastersyllabii/&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ext=&quot;.pdf&quot;;}<br>&nbsp;&nbsp;else if (document.forms[0].elements.dowhat[2].checked == true)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{path=&quot;coursesyllabii/&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ext=&quot;c.pdf&quot;;}<br>&nbsp;&nbsp;else if (document.forms[0].elements.dowhat[3].checked == true)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{path=&quot;mastersyllabii/&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ext=&quot;.rtf&quot;;}<br>&nbsp;&nbsp;else <br>&nbsp;&nbsp;&nbsp;&nbsp;{path=&quot;coursesyllabii/&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ext=&quot;c.rtf&quot;;}<br>&nbsp;}<br>return path,ext;<br>}<br>// --&gt;<br>&lt;/script&gt;<br>.<br>.<br>.<br>Some HTML<br>.<br>.<br>.<br>The following are the radio buttons with which the user selects what he/she wants to do.<br><br>&lt;form method=&quot;post&quot;&gt;<br>&nbsp;&nbsp;&lt;table width=&quot;100%&quot; border=&quot;0&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;50%&quot; valign=&quot;top&quot;&gt; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;&lt;font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot; color=&quot;#004C00&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;u&gt;View&lt;/u&gt;&lt;br&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;radio&quot; name=&quot;dowhat&quot; onclick=&quot;form.dowhat.value='viewd';&quot; value=&quot;viewd&quot; checked&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Course Description and Learning Outcomes&lt;br&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;radio&quot; name=&quot;dowhat&quot; value=&quot;viewm&quot; onClick=&quot;form.dowhat.value='viewm';&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Master Syllabus (Acrobat Reader Required)&lt;br&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;radio&quot; name=&quot;dowhat&quot; value=&quot;viewc&quot; onclick=&quot;form.dowhat.value='viewc';&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Course Syllabus (Acrobat Reader Required) &nbsp;&lt;a href=&quot;<A HREF=" TARGET="_new"> src=&quot;images/getacro.gif&quot; width=&quot;88&quot; height=&quot;31&quot; alt=&quot;Acrobat Reader&quot; border=&quot;0&quot; align=&quot;top&quot;&gt;&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;<br>&nbsp;&nbsp;&lt;/td&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;50%&quot; valign=&quot;top&quot;&gt;&lt;font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot; color=&quot;#004C00&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;u&gt;Download&lt;/u&gt;&lt;br&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;radio&quot; name=&quot;dowhat&quot; value=&quot;dwnldm&quot; onclick=&quot;form.dowhat.value='dwnldm';&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Master Syllabus (RTF Format) &lt;br&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;radio&quot; name=&quot;dowhat&quot; value=&quot;dwnldc&quot; onclick=&quot;form.dowhat.value='dwnldc';&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Course Syllabus (RTF Format) &lt;br&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/font&gt;&lt;/td&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;<br>&nbsp;&nbsp;&lt;/table&gt;<br>&lt;/form&gt;<br>&lt;table&gt;<br>.<br>.<br>.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A bunch of table rows with javascript URL's<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;similar to the one below.&nbsp;&nbsp;What I am doing here is:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Checking to see if popup win1 exists, then closing it if it does.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Call function dothis() to generate the path and extension portions of the URL.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;After returning from dothis(), I call function dfn() which calls the popup, views the pdf, or downloads the rtf based on user input (the radio buttons)<br>.<br>.<br>.<br><br>&lt;tr&gt; <br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;48%&quot; height=&quot;24&quot;&gt; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div align=&quot;left&quot;&gt;&lt;a href=&quot;javascript:;&quot; onclick=&quot;javascript:((win1)?win1.close():x=1);dothis();(dfn(path+'m106'+ext,title,detail))&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot; color=&quot;#004C00&quot;&gt;MET 106 Survey of Commercial HVAC Systems&lt;/font&gt;&lt;/a&gt;&lt;/div&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;4%&quot; height=&quot;24&quot;&gt;&nbsp;&nbsp;&lt;/td&gt;&nbsp;&nbsp;&nbsp;&nbsp;(Blank Cell)<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;48%&quot; height=&quot;24&quot;&gt; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Another javascript URL<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;<br>&nbsp;&nbsp;&lt;/tr&gt;<br>.<br>.<br>.<br>Rest of Table<br>.<br>.<br>.<br>&lt;/table&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top