0

I have the following php code:

<span><?php if (!empty($post_file)){echo "PDF FILE: &nbsp&nbsp&nbsp&nbsp&nbsp <a href='./pdf/$post_file' title='PDF' onclick='pdfOpen();' rel='nofollow'><img src='./images/acrobat-reader.png' height='22px;' width='19px;'></a>";}?></span> 

and the Javascript function for onclick event:

<script> function pdfOpen(){ window.open(this.href,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no'); return false; }; </script> 

The problem is when i clicked the img the pdf file open normally in the initial page and then opens a new window as I set in my Javascript code but the new window is blank...No url, no pdf, nothing.. How I can fix it? Thanks a lot for your effort?

2

2 Answers 2

1

The problem occurs because of the code snippet used for the onlick handler:

 onclick="pdfOpen()" 

This string compiles into a function used to set the link's onclick property:

 function( event) { pdfOpen(); } 

which calls pdfOpen without a this value set to the anchor element. Try

 onclick="return pdfOpen(this)" 

in the <a> tag, in combination with

 function pdfOpen( anchor){ window.open(anchor.href,'win2', 'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no'); return false; }; 

You could, of course, set up and call event.preventDefault instead of returning false from the onclick handler to prevent default event handling.

A preferable alternative could be to add or register event handlers in script executed after the DOM is ready. This avoids problems associated with code snippets in HTML which were designed to work under HTML 3.

Sign up to request clarification or add additional context in comments.

Comments

1

Ordinarily, the simplest way to achieve this would be to use target="_blank" in the a tag. But as was pointed out here, that's not guaranteed to work. It depends on the user's browser settings.

1 Comment

Thank a lot my friend I fix it.The problem was that i must put $(document).ready() function so the pdfOpen() can use the href as it set to the anchor element.Your advice helped me to fix an another thing,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.