Auto-Launch a file attachment

Jake Howlett, 24 October 2000

Category: Views; Keywords: file attachment launch

In the Notes Client there is an option when designing a form to tell it to launch the first attachment when a document is opened. How about doing this in the browser? I've thought of two ways of doing it:

  1. Meta-Tags
  2. JavaScript
1. Meta Tags - Using the Refresh meta-tag we can tell the browser to redirect the page to the chosen attachment.

Place the following line in the $$HTMLHead field (or its equivalent in R5)

path := @ReplaceSubstring(@Subset(@DBName; -1); "\\"; "/");
file := @ReplaceSubstring(@Subset(@AttachmentNames; 1); " "; "+");
@If(@Attachments;
"<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;URL=/" + path + "/0/" + @Text(@DocumentUniqueID) + "/$file/" + file + "\">"
;"")


When the page is opened it automatically opens the first attachment. What happens next depends on the browser, the file type and the user's preferences. In IE, if the file is an office document it will probably launch automatically. Text (.txt) files should open on all browsers as should Adobe Acrobat (.pdf) files and standard HTML files. The user also has the option to tell the browser what to do with each type of file.

2. JavaScript This method is number two as it is not as fool proof as the meta-tag method. Place the following somewhere in your header ($$HTMLHead field) and as soon as the browser encounters the JavaScript that is generated it will redirect to the attachment.


path := @ReplaceSubstring(@Subset(@DBName; -1); "\\"; "/");
file := @ReplaceSubstring(@Subset(@AttachmentNames; 1); " "; "+");
@If(@Attachments;
"<script type=\"text/javascript\">"+@NewLine+
"<!-- "+@NewLine+
"location.replace(\'/" + path + "/0/" + @Text(@DocumentUniqueID) + "/$file/" + file + "\');"+@NewLine+
" -->"+@NewLine+
"</script>"
;"")


JavaScript that is generated:

<script type="text/javascript">
location.replace( '/dir/db.nsf/0/1d45..49de/$file/test.txt' );
</script>


Note: This could be done simply by changing the value of location.href but there is a good reason not to. If you simply change the location href then the "redirect" page is still in the browser's history and every time the user hits the back button they will simply re-run the above script and end up back in the page they were already in. Hence, "breaking the back button" = very annoying. Using the replace method tells the browser not to remember the current page and make the back button go to the page they were in before the "redirect" page.