0

Is there a way to pass url parameters to a web server when calling window.open() on a file (.doc)?

My problem is that when I call window.open to open a file (on c:) from a javascript function, a request is made to the (coldfusion) web server to reload the current page (which is weird b/c I'm not even using a submit type input):

viewAttachments.cfm

function viewFile(selectbox) {
    var selItem  = selectbox.options[selectbox.selectedIndex].text;
    var selValue = selectbox.options[selectbox.selectedIndex].value;
    var filePath = selValue + '\\' +  selItem;
    window.open(filePath);
}

<cfform name="gridForm"  method="post">
<table>
    <tr><td>
        <cfset destination = expandPath("./cold_case_files")>
        <cfdirectory directory="#destination#" action="list" name="fileList" type="file" >  
        <select id="fileName" name="fileName">
            <cfoutput>
                <cfloop query="fileList">
                    <option value="#directory#" >#fileList.Name#</option>
                </cfloop>
            </cfoutput>
        </select>  
    </td></tr>
    <tr><td>
        <input type="image" src="btn.jpg" name="view" onClick="viewFile(fileName);return false;" />
    </td></tr>
</table>
</cfform>

This is a problem b/c there are coldfusion references to the url parameters throughout the document (passed in when the page is first loaded), which causes a "url parameter not found" exception:

var ProjID = <cfoutput>#url.ProjID#</cfoutput>;

I tried calling window.open with the various values for the name parameter to prevent the viewAttachments.cfm from reloading:

window.open(URL,name,specs,replace)

  • _blank - URL is loaded into a new window. This is default
  • _parent - URL is loaded into the parent frame
  • _self - URL replaces the current page
  • _top - URL replaces any framesets that may be loaded

So my two options are either to prevent the calling page to reload when window.open() is fired, or figure out how to pass in url parameters to the call.

The really weird thing is that the page works on the production site that I copied the code from. I was told by the upper brass that it is not a web server config or application settings issue, so I'm at a loss.

Gratzi.

Community
  • 1
  • 1
samus
  • 6,102
  • 6
  • 31
  • 69
  • 1
    Oh, maybe I need to get rid of the `method="post"` statement. – samus Mar 02 '16 at 15:00
  • ... didn't work, still posts for some reason ... – samus Mar 02 '16 at 15:02
  • What is `fileName` at `onClick="viewFile(fileName);` ? – guest271314 Mar 02 '16 at 15:08
  • path to a word doc or spreadsheet, sorry, fixing... – samus Mar 02 '16 at 15:11
  • Note, have not tried `coldfusion` though appears _"path to a word doc or spreadsheet"_ at `window.open()` requests the file ? Does request of any file from server reload current page that started request ? – guest271314 Mar 02 '16 at 15:16
  • Yes, that is exactly what is happening. – samus Mar 02 '16 at 15:19
  • 1
    One option could be to use `data URI` representing file at `window.open()` , which should not make a request to the server – guest271314 Mar 02 '16 at 15:26
  • Investigating data URI file representation (thanks!!) ... – samus Mar 02 '16 at 15:34
  • In your function `viewFile` you are setting a variable named `filePath` but your `window.open()` method is using a variable named `newPath`? The `window.open()` method should not be calling your ColdFusion server to process a Word document. What is the actual URL that is being called? – Miguel-F Mar 02 '16 at 15:56
  • window.open("c:\path-to-word-doc") (`newPath=filePath.replace(...)` updated) – samus Mar 02 '16 at 16:21
  • So you are opening a local file. I did not think browsers allowed that. – Miguel-F Mar 02 '16 at 17:11
  • (Edit) @SamusArin - You are passing in a physical file path, rather than a valid URL pointing to the file on the server ie `http://server/path-to-word-doc.docx`. *RE: I did not think browsers allowed that* .. and it is moot point anyway because even if it was allowed, it still would not work here because the browser would be looking for the file on the *client* computer, not the server. – Leigh Mar 02 '16 at 17:12
  • *still would not work* ... except maybe if executed in a browser on the CF server itself. – Leigh Mar 02 '16 at 17:19
  • That's an absolutely valid point about the file location, I just didn't get far enough for the server to complain about it yet. That as well as this code working in a production environment (it's an "intranet/ie" webapp). – samus Mar 02 '16 at 18:12
  • We'll regardless of what is going on with the production code, using `http://server/path-to-doc.docx` works just peachy. Thanks guest271314 and Leigh (whoever wants the street cred just put it in an answer). I should've just tried this, but how far I can think outside the box is a function of my anger, I need to work on this. – samus Mar 02 '16 at 18:36

1 Answers1

1

(From comments...)

You are passing in a physical path to the file, ie c:\path\someFile.docx. Instead, you need to use a URL which points to that file on the server, ie http://yourserver/path/to/someFile.docx.

As far as the original code, I would be surprised if it worked. Not all browsers support accessing local files. The ones that do usually require special permissions and syntax, ie "file:///...., which the code is not using. (Note: HTML5 capabilities are different). Plus the files are not local anyway. They are located on the CF server. However, the browser would be looking on the local client computer. So it would almost always fail. Unless perhaps you opened that page in a browser - on the CF server itself - which your average users obviously cannot do ;-)

Community
  • 1
  • 1
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • I don't know either Leigh. All's I can say is that the Lord works in mysterious ways sometimes (I did consider doing this at some point, but was more worried about making any alterations.) – samus Mar 03 '16 at 14:34