www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Open and Close Javascript Window (to download a file)

  1. #1
    Join Date
    Nov 2009
    Posts
    8

    Open and Close Javascript Window (to download a file)

    Hey all,

    New to JS, but have got this going.

    I want to download a file via javascript (basically to hide the location of the file), so I have in the A tag:

    <A HREF="javascript:void(0)"
    onClick="window.open('http://greenbutton.com/downloads/test.zip','download','width=1, height=1')">

    It works! But then there's a tiny window that doesn't close. Is there anyway to get this window to close from the "parent?" I'll need to have this working for about 16 links on one page...

    Many thanks!

    H

  2. #2
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    I'm assuming you know this doesn't really hide the file location - anyone could find it in your source code.

    Yes, you can close windows from parents (or indeed from anywhere) but you need to set up your pop-up windows properly to do this - i.e. with names). Better to call a function from your onclick and open the window from there, giving it a name. Then, via that name, you can close it, write to it, reposition it, whatever. Look up any pop-up window script online and you'll see this - the name is one of the arguments you pass to window.open().

    I'd be inclined to do what you're doing with a hidden iframe - saves the ugliness of an appearing then disappearing pop-up.

  3. #3
    Join Date
    Nov 2009
    Posts
    8
    Yes, I totally realize it's be revealed in the source - not needing a super secure solution here. Hidden iframe! That sounds great! How wold I do that?

    Many thanks!

  4. #4
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    Just have an iframe with display:none set then literally just point your link at it (in the target="" attribute). Then the download prompt will come up (depending on the filetype of the downloaded file) and look as though it came straight from the main page.

  5. #5
    Join Date
    Nov 2009
    Posts
    8
    So I tried to get it working like this:

    In the header:
    <script type="text/javascript">
    function changeme(id, action) {
    ****** if (action=="hide") {
    ************document.getElementById(id).style.display = "none";
    ****** } else {
    ************document.getElementById(id).style.display = "block";
    ****** }
    }
    </script>

    In the body:
    <a href="javascript:void(0)" onclick="changeme('if1', 'block'); class="downloaditem"><img src="images/zipicon.png" class="zip" alt="zip" /> Number 1, 85 Tracks, 5mb</a></li>
    <iframe style="display: none;" id="if1" src="http://greenbutton.com/downloads/test.zip"></iframe>

    The file downloads on page load even though the iframe is hidden? What am I missing? Thanks again!

  6. #6
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    The browser doesn't care that it's hidden - as far as it's concerned, it's a valid HTTP gateway and has all the loading capability and DOM structure of the main page. Glad you got it sorted.

  7. #7
    Join Date
    Nov 2009
    Posts
    8
    Thanks for your reply. Is there anyway to not download the file on page load? I want it to download once somebody clicks the link, not on page load...

    (Even if I hide the iframe, you're saying it'll download?)

    Thanks again!

  8. #8
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    Yes, it'll definitely download.

    I didn't suggest anything which would make the download begin onload as opposed to on a link click. As I said, you simply need to target your download link to the iframe. So for example:

    Code:
    <a href="someFile.zip" target="ifr">Click here to download</a>
    <iframe name="ifr" width="1" height="1"></iframe>

  9. #9
    Join Date
    Nov 2009
    Posts
    8
    Thanks again for your help!

    I think I'm not being clear....

    I want a link like <a href="javascript:void(0) ...> so the file (ie someFile.zip) isn't revealed in the status menu (which is why I'd rather not link to the file in the first place.) I was thinking you meant for the link onclick to reveal the hidden iframe, (which would have src="someFile.zip) hence the file would download.

    What I'm experiencing is that the file downloads on page load, which I don't want. (I want the user to have to click the link.

    Does this make sense? Is there a practical way to do this? Iframe or not, all I basically need is a way to download a file via javascript....

    Many thanks again!!!

  10. #10
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    It makes no sense at all that anything should happen onload because the only reference to the file is in the link.

    Re: my last post - I forgot you wanted it onclick, not in the href. Fine, just put this:
    >

    To be clear: that goes in your MAIN document, not in the iframe.

    That will launch the file download within the iframe. Nothing should be happening onload; after all, there's no onload="..." anywhere!

  11. #11
    Join Date
    Nov 2009
    Posts
    8
    Thanks for your reply!

    All I see in your last post was >


  12. #12
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    Eh? Do you think someone upstairs perhaps doesn't want you to achieve this? I wrote a full reply, and I can see it fine. Bizarre.

    Here it is agian:

    ------------

    It makes no sense at all that anything should happen onload because the only reference to the file is in the link.

    Re: my last post - I forgot you wanted it onclick, not in the href. Fine, just put this:

    To be clear: that goes in your MAIN document, not in the iframe.

    That will launch the file download within the iframe. Nothing should be happening onload; after all, there's no onload="..." anywhere!

  13. #13
    Join Date
    Nov 2009
    Posts
    8
    Mitya,

    You won't believe this, but all I can see is:

    It makes no sense at all that anything should happen onload because the only reference to the file is in the link.

    Re: my last post - I forgot you wanted it onclick, not in the href. Fine, just put this:

    To be clear: that goes in your MAIN document, not in the iframe.

    That will launch the file download within the iframe. Nothing should be happening onload; after all, there's no onload="..." anywhere!

    !!!

  14. #14
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    I'm so confused! You just posted my message back to me. It looks correct, like my message. I don't understand what's wrong. Look in the source code, it's there! Arrghhhh!

  15. #15
    Join Date
    Jan 2009
    Posts
    3,346
    It looks like the reason the file is downloading "onload" is because the OP has this for the iframe:
    Code:
    <iframe style="display: none;" id="if1" src="http://greenbutton.com/downloads/test.zip"></iframe>
    Note the "src" attribute.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles