Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    javascript onclick=window.open, problem with mouse right-hand context menu

    Using onclick=window.open function in js to open a pdf file link in a new popup window.

    Works fine to display the file onscreen, but not if the user wants to save the file client-side to their computer.

    The right-hand-button context menu for the mouse will allow the user to download, but the file saved will be a html dump file for the webpage
    and the name of the file will be that for the webpage.
    Of course I can use the easy <a href> method for download links and the mouse context menu options will be as expected,
    but I can only use target="-blank" or target="_self".
    I need a popup window to open.

    Could use :
    oncontextmenu="alert('Left click the link to open, and then SAVE from with the pdf viewer')
    to advise users how to save the file, and could use
    "javascript: void(0)"
    to eliminate most mouse context menu options, so the user won't bother try.

    So how can I get a link to a file which can be viewed in a popup window and downloaded using mouse right-hand context menu?

    Any advice massively appreciated!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,133
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    I dunno if this will work, but have you tried:
    Code:
    <a href="whatever.pdf" target="PDF" rel="nofollow"
       onclick="window.open('','PDF','...whatever...'); return true;">
    That should open a blank window, with the characteristics you want, because of the onclick, then the return true says "go ahead with the normal href action".

    Or did I completely misunderstand what you are saying?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,133
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Yeah, I think that does what you want. Just tried it.

    Click on it, opens the PDF in a new window.

    Right click on it and you can do "Save target as..." and it works.

    Tried with MSIE and FF.

  • Users who have thanked Old Pedant for this post:

    Jon Dalton (08-18-2011)

  • #4
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks Old P for getting back to me

    Advance apologies if i'm getting confused, but after trying your method I cannot get the desired behaviour.

    Whats wanted
    ...
    A textual/graphical link to a PDF file needs to perform two functions :

    1. When left-clicked a new popup window should appear that displays the contents of the PDF file. But the contents of the PDF file should not be displayed in the existing browser window, nor should it be displayed in a new browser window (aka new tab).

    2. When right-clicked the context menu options need to include "Save link as / Save taget as ..." and save the actual PDF file itself, not save a html dump of the wesite itself.

    Using your method I am still getting a new window opening in addition to the popup window opening.

    Guess I need to use <a href> to tell the browser that right-click options are needed, but not to treat the link as a left-clickable link. Then use <onlick> to load the required popup window.
    Last edited by Jon Dalton; 08-18-2011 at 03:28 PM. Reason: rewrite

  • #5
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ignore this .....................
    Last edited by Jon Dalton; 08-18-2011 at 03:32 PM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,133
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    1. When left-clicked a new popup window should appear that displays the contents of the PDF file. But the contents of the PDF file should not be displayed in the existing browser window, nor should it be displayed in a new browser window (aka new tab).

    2. When right-clicked the context menu options need to include "Save link as / Save taget as ..." and save the actual PDF file itself, not save a html dump of the wesite itself.
    I dunno what to say. It worked *EXACTLY* like that for me.

    The only "trick" to it is that you *MUST* specify the characteristics of the popup window as I showed, especially the width and height. If you don't then the browser can indeed choose to give you a new tab instead of a new window.

    The *EXACT* code I used in testing it was this:
    Code:
    <html>
    <body>
    <a href="junk.pdf" rel="nofollow" target="PDF"
       onclick="window.open('','PDF','width=600;height=800;scrollbars=yes');return true;">
    DO IT TO ME
    </a>
    </body>
    </html>
    If I left clicked on "DO IT TO ME", it opened up the "junk.pdf" file in a popup window sized at 600x800 pixels. (Except that Chrome opened a new window, but not that size, but at least it was a new window.)

    If I right clicked on "DO IT TO ME", it opened up a context window and I could do "Save link as..." with Firefox and Chrome or "Save target as..." in MSIE.

    What am I missing???

  • #7
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Resolved

    RESOLVED

    You're absolutely right of course, I was not observing type case.

    I was using :

    Code:
    <html>
    <body>
    <a href="junk.pdf" rel="nofollow" rel="nofollow" target="PDF"
       onclick="window.open('','pdf','width=600;height=800;scrollbars=yes');return true;">
    DO IT TO ME
    </a>
    </body>
    </html>
    which caused the problem.

    Cheers for the help Old Pedant , me thinks I had better buy me a heavyweight Javascript Text Book. (to knock a bit of sense into my head next time!).

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,133
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    LOL! Nah, *everybody* has made that upper/lower case mistake sometime in their coding career. My favorite "trick" is to camel case a variable one place (e.g., showTabs) and then do it all lower case another place (e.g., showtabs). Thank goodness for debuggers.

    Anyway, this really wasn't so much a JavaScript question as it was knowing how to "fool" the browser into forcing it to open a window in the way you wanted. I learned about opening an empty-but-named window a *long* time ago (worked even in the days of MSIE 4 and I think MSIE 3).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •