Skip to main content

Download File Using jQuery



How can I prompt a download for a user when they click a link.





For example, instead of:







<a href="uploads/file.doc">Download Here</a>







I could use:







<a href="#">Download Here</a>



$('a').click... //Some jquery to download the file







This way, Google does not index my HREF's and private files.





Can this be done with jQuery, if so, how? Or should this be done with PHP or something instead?





Thanks!



Source: Tips4all

Comments

  1. I might suggest this, as a more gracefully degrading solution, using preventDefault:

    $('a').click(function(e) {
    e.preventDefault(); //stop the browser from following
    window.location.href = 'uploads/file.doc';
    });

    <a href="no-script.html">Download now!</a>


    Even if there's no Javascript, at least this way the user will get some feedback.

    ReplyDelete
  2. If you don't want search engines to index certain files, you can use robots.txt to tell web spiders not to access certain parts of your website.

    If you rely only on javascript, then some users who browse without it won't be able to click your links.

    ReplyDelete
  3. Here's a nice article that shows many ways of hiding files from search engines:


    http://www.antezeta.com/blog/avoid-search-engine-indexing


    JavaScript isn't a good way not to index a page; it won't prevent users from linking directly to your files (and thus revealing it to crawlers), and as Rob mentioned, wouldn't work for all users.
    An easy fix is to add the rel="nofollow" attribute, though again, it's not complete without robots.txt.

    <a href="uploads/file.doc" rel="nofollow">Download Here</a>

    ReplyDelete
  4. Yes, you would have to change the window.location.href to the url of the file you would want to download.

    window.location.href = 'http://www.com/path/to/file';

    ReplyDelete
  5. See here for a similar post on using jQuery to clear forms: Blank out a form with jQuery

    You may also be running into an issue where the values are being repopulated by the struts value stack. In other words, you submit your form, do whatever in the action class, but do not clear the related field values in the action class. In this scenario the form would appear to maintain the values you previously submitted. If you are persisting these in some way, just null each field value in your action class after persisting and prior to returning SUCCESS.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.