Close pop-up window on a 'Download' button click

Discussion in 'PHP' started by tk421, Feb 4, 2010.

  1. #1
    A bit of a newb here so please have mercy on me... My problem involves php code so I thought I'd ask here.

    I've read some of the threads here about closing a pop-up as a secondary action but the replies were very cursory (suggested adding an onclick attribute to a link) so I am hoping someone can shed more detailed light on the issue.

    I have a java pop-up window for downloading selected mp3s from my database (I know some will say I don't need a pop-up window to link to a simple download but for the design of my site I really want to do it this way).

    I need my custom 'download' button to not only trigger the normal browser download dialog (which it does perfectly) but I need the pop-up window to close on the same button click.

    The problem is (I think) that the href is embedded in php and I'm not sure where to put the 'onclick' code.

    Here is the line of code that tells the 'download' button what to do:

    <td align="center" colspan="2"><?php echo '<a href="' . tep_href_link(FILENAME_FREE_DOWNLOAD, 'id=' . $product_attributes['products_attributes_id']) . '">' . tep_image_button('button_startfreedownload.jpg', IMAGE_BUTTON_CONTINUE) . '</a>'; ?> </td>

    I tried every combination of inserting "onClick="javascript:window.close();" in various places in the above code to no avail. I am sure I just do not have a clue where to put it or even if it is the right code to use.

    I really hope someone can help. I can post more of the code if necessary.

    thanks so much--
    yours humbly,
    matt
     
    tk421, Feb 4, 2010 IP
  2. DEWebDev

    DEWebDev Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this and see if it works:

    <td align="center" colspan="2"><?php echo '<a href="' . tep_href_link(FILENAME_FREE_DOWNLOAD, 'id=' . $product_attributes['products_attributes_id']) . '" onClick="javascript:window.close();">' . tep_image_button('button_startfreedownload.jpg', IMAGE_BUTTON_CONTINUE) . '</a>'; ?> </td>
    PHP:
     
    DEWebDev, Feb 4, 2010 IP
  3. tk421

    tk421 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for that... I tried it and it did close the pop-up but the download dialog never opened.

    -matt
     
    tk421, Feb 4, 2010 IP
  4. tk421

    tk421 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Let me say though that as I hover the mouse over the 'download' button the proper link does show at the bottom of the pop-up window: exactly the same link as when the download dialogue was opening properly (no mention of the 'onlick' code down there after pasting in your suggestion).

    not sure if that helps...
    -matt
    :confused:
     
    tk421, Feb 4, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Use php headers download.
     
    danx10, Feb 5, 2010 IP
  6. tk421

    tk421 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I have been looking into php header downloads. It looks like I would have to re-structure my pop-up completely to use this method.

    I am not the original author of most of the code on my site so I may not posses the level of code knowledge to make such major changes.

    Short of using php header downloads is there a less elegant solution using 'onclick' dialogue with my current code? The download seems to work just fine with all major browsers so I really hate to change everything unless there is absolutely no other way to auto-close the pop-up.

    thanks
    matt
     
    tk421, Feb 5, 2010 IP
  7. lovingwings36

    lovingwings36 Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This should do the trick for you.

    <td align="center" colspan="2"><?php echo '<a href="' . tep_href_link(FILENAME_FREE_DOWNLOAD, 'id=' . $product_attributes['products_attributes_id']) . '">' . tep_image_button('button_startfreedownload.jpg', IMAGE_BUTTON_CONTINUE) . ' onClick=\"java_script:window.close();\"</a>'; ?> </td>
    Code (markup):
     
    lovingwings36, Feb 15, 2010 IP
  8. Marshton

    Marshton Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If the dude's above doesnt work, try mine:

    <td align="center" colspan="2"><?php echo '<a href="' . tep_href_link(FILENAME_FREE_DOWNLOAD, 'id=' . $product_attributes['products_attributes_id']) . '" onClick="javascript:settimeout(\"window.close();\",500)">' . tep_image_button('button_startfreedownload.jpg', IMAGE_BUTTON_CONTINUE) . '</a>'; ?> </td>
    PHP:
    It just add's a timeout to the window closing, hopefully giving some time for the download dialog to open
     
    Marshton, Feb 16, 2010 IP
  9. tk421

    tk421 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for both suggestions guys. Unfortunately this problem is proving to be quite the sticky puzzle.

    I first pasted 'lovingwings36's code in place of my own. It simply displayed the 'onClick=\"java_script:window.close()' as text to the right of my button and did not function.

    I tried moving the close window code before the button image reference (where I'm kinda thinking it should be) - It still did not work but at least it was not printing the close code as text.

    NEXT I tried the line of code from 'Marshton' with the settimer close function. The download dialogue worked fine but the pop-up never closed. (is the /500 referring to 500 milliseconds by the way?) I tried following through with the full mp3 download process after-which I closed the stock download window but the pop-up still never closed.

    The CLOSEST code to working is the following (without escaped quotes):

    <td align="center" colspan="2"><?php echo '<a href="' . tep_href_link(FILENAME_FREE_DOWNLOAD, 'id=' . $product_attributes['products_attributes_id']) . '" onClick="javascript:window.close()">' . tep_image_button('button_startfreedownload.jpg', IMAGE_BUTTON_CONTINUE) . '</a>'; ?> </td>


    With that code - clicking on my homemade DOWNLOAD NOW button simply closes the pop-up without opening the download dialogue - HOWEVER if you right click on the homemade button (with my above code) and 'open in a new window' the download dialogue box opens and functions but, of course you are still left with the pop-up window AND a full size blank new browser window.

    If I use the above code with slashes to escape the quotes such as:
    onClick=\"javascript:window.close()\"
    the download works but the pop-up will not close.

    So the above code (in red) seems to allow both functions to work just not at the same time. It seems I am so close but I MUST be missing some simple little thing. UUGGHH!

    Any ideas? (thanks for all the help so far BTW)
    -matt :confused:
     
    tk421, Feb 16, 2010 IP