...

View Full Version : javascript onclick=window.open, problem with mouse right-hand context menu



Jon Dalton
08-17-2011, 06:16 PM
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. :rolleyes:

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!

Old Pedant
08-18-2011, 02:45 AM
I dunno if this will work, but have you tried:


<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?

Old Pedant
08-18-2011, 02:53 AM
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.

Jon Dalton
08-18-2011, 04:19 PM
Thanks Old P for getting back to me :thumbsup:

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.

Jon Dalton
08-18-2011, 04:27 PM
Ignore this .....................

Old Pedant
08-18-2011, 09:59 PM
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:


<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???

Jon Dalton
08-19-2011, 12:30 PM
RESOLVED

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

I was using :


<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 :thumbsup:, me thinks I had better buy me a heavyweight Javascript Text Book. (to knock a bit of sense into my head next time!).

Old Pedant
08-19-2011, 08:56 PM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum