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?

Musa
  • 89,286
  • 16
  • 105
  • 123
liontass
  • 652
  • 7
  • 20
  • http://stackoverflow.com/questions/4195970/what-does-this-mean – Musa Jan 06 '17 at 21:56
  • Possible duplicate of [Opening a new tab to read a PDF file](http://stackoverflow.com/questions/16925481/opening-a-new-tab-to-read-a-pdf-file) – shalvah Jan 06 '17 at 22:03

2 Answers2

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.

Community
  • 1
  • 1
shalvah
  • 824
  • 8
  • 18
  • 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, – liontass Jan 06 '17 at 22:14
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.

traktor
  • 12,838
  • 3
  • 23
  • 44