0

I am trying to store clicked link in database

<a id="downloads" href="http://example.com/filename?hello.rar">Download</a> $('#downloads').click(function(){ event.preventDefault(); var url ="http://www.example.com/count.php?filename=hello.rar"; $.get(url, function(data) {}); }); 

Link gets stored in database but file download don't start http://example.com/filename?hello.rar if i remove event.preventDefault(); download start but it don't save link in database

Please provide some solution to this or Suggest any better way to do this. Thanx

0

1 Answer 1

1

#1

Change CLICK listener to listen for a class or else you can only have one link

#2

Do what you see below

Something like this should work:

HTML

<a class="downloads" href="http://example.com/filename?hello1.rar" target="_blank">Download 1</a> <a class="downloads" href="http://example.com/filename?hello2.rar" target="_blank">Download 2</a> <a class="downloads" href="http://example.com/filename?hello3.rar" target="_blank">Download 3</a> 

jQuery

$('.downloads').click(function(){ var clickedURL = $(this).attr('href'); $.get('http://www.example.com/yourSaveURLToDatabaseScript', {'url': clickedURL}); }); 
Sign up to request clarification or add additional context in comments.

8 Comments

See edit, open the download link in a new tab. Most browsers are set to close the tab if they detect a file download like this.
thanx a ton.i was search whole night for this.
just take note that, this is not a reliable way of tracking downloads, you can just extract http logs from your web server for a more accurate statistics..
@YogeshLakhotia it depends on what web server you are using, a high level design would be to use regex to extract the statistics
it all really depends on what web server and how the logs are written, saving to db using ajax is not the best way to go definitely, i'd rather use google analytics events to track those
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.