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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

window.open displayed [object] in calling window

Status
Not open for further replies.

new2unix

Programmer
Feb 5, 2001
143
US
Hi,

The following is a portion of my template that would display links based a directory list, in this case, the files are all pdf document.

<CFDIRECTORY
ACTION=&quot;list&quot;
DIRECTORY=&quot;#GetDirectoryFromPath(GetBaseTemplatePath())#\#RefDir#\#RefDirSelect#&quot;
NAME=&quot;myRefSubDir&quot;
SORT=&quot;name ASC, size DESC&quot;>

<CFLOOP QUERY=&quot;myRefSubDir&quot;>
<tr>
<td><A HREF=&quot;javascript:window.open('#RefDir#/#refDirSelect#/#NAME#')&quot;><IMG SRC=&quot;images/rt_arrow_circle.gif&quot; WIDTH=33 HEIGHT=33 BORDER=0 ALT=&quot;#NAME#&quot;></A></td>

<td width=&quot;90%&quot;><STRONG><A HREF=&quot;javascript:window.open('#RefDir#/#refDirSelect#/#NAME#')&quot;><FONT face=Verdana>#NAME#</FONT></STRONG></A></td>
</tr>
</CFLOOP>

When clicking on the link, a separate window opens up to display the pdf document. But in the original directory list page, instead of still displaying the file list with link, the page became blank with a line of text:

[object]

As a result, I no longer have any links available to open a different pdf document.

What did I do wrong?

Thanks

Mike
 
I was doing much the same kind of program today. I use a similar method. Note how I test if the user has selected a pdf file, how the program opens the file and returns to the first page with listings, and how it returns to the first page without opening the file should it not be a pdf:


<cfif #FINDNOCASE( &quot;pdf&quot;, directory_list.node)#>
<cfset gotostring1=&quot;#destination3#&quot;>
<cfset gotostring2='&quot;'&#gotostring1#&'&quot;'>
<script Language=&quot;JavaScript&quot;>
function OpenWindow(){
window.open(#gotostring2# ,'w1','width=800,height=600,resizable=1,status=1,scrollbars=1');
history.back();
}
</script>
<body onLoad=&quot;OpenWindow();&quot;>
<cfelse>
<script>
history.back()
</script>
</cfif>
</cfoutput>
 
What you really need to do in this case is properly handle the result of the
Code:
window.open()
method to a variable. The issue here is that the
Code:
window.open()
method returns an object reference (in fact, a reference to the
Code:
window
object of the new window), and when an object reference is returned to the browser where it expects an HREF, it's sort of choking and displaying the text [ignore][object][/ignore].

Instead of calling the
Code:
window.open()
method in the HREF, define a function to do that, and call the function in the HREF, like so:
Code:
<script type=&quot;text/javascript&quot; language=&quot;JavaScript&quot;>
   var refNewWin;
   function popWin(strUrl)
   {
      refNewWin = window.open(strURL,&quot;newWin&quot;);
   }
</script>

<cfoutput>
   <a href=&quot;#RefDir#/#refDirSelect#/#NAME#&quot; target=&quot;_blank&quot; onclick=&quot;javascript:popWin('#RefDir#/#refDirSelect#/#NAME#');return false;&quot;>
      <img src=&quot;images/rt_arrow_circle.gif&quot; width=&quot;33&quot; height=&quot;33&quot; border=&quot;0&quot; alt=&quot;#NAME#&quot; />
   </a>
</cfoutput>

Notice a few things here.[ul]
[li]First, I'm assigning the reference to the new window to a variable,
Code:
refNewWin
, and that this is a variable that is not local to the function, so it persists after the function call and can be used to do things like check whether the new window is open or to close it programmatically.[/li]
[li]Second, I've put the direct path to the file in the HREF attribute, and put the javascript call in the onclick handler. The
Code:
return false;
after the function call means that if javascript is supported on the browser, the onclick handler will fire (thus opening your new window) and the browser will not go to the HREF specified. If javascript is turned off, the file will open in a new window per the browser's default. This provides a fallback for users with JavaScript turned off. In fact, you should avoid putting
Code:
javascript:
function calls that have dynamic parameters in the HREF attribute wherever possible, since some characters cause problems in Netscape.[/li]
[/ul]Hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top