Web Analytics Made Easy -
StatCounter javascript onclick=window.open, problem with mouse right-hand context menu - CodingForum

Announcement

Collapse
No announcement yet.

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

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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?
    Be yourself. No one else is as qualified.

    Comment


    • #3
      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.
      Be yourself. No one else is as qualified.

      Comment


      • #4
        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; Aug 18, 2011, 10:28 AM. Reason: rewrite

        Comment


        • #5
          Ignore this .....................
          Last edited by Jon Dalton; Aug 18, 2011, 10:32 AM.

          Comment


          • #6
            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???
            Be yourself. No one else is as qualified.

            Comment


            • #7
              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('','[COLOR="DarkRed"][B]pdf[/B][/COLOR]','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!).

              Comment


              • #8
                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).
                Be yourself. No one else is as qualified.

                Comment

                Working...
                X
                😀
                🥰
                🤢
                😎
                😡
                👍
                👎