Go Down

Topic: HTML, Ajax, JavaScript... question (Read 1 time) previous topic - next topic

Nachtwind

Hi there..
just a small question - does someone here know if it is doable (and if so - how?) to make Download Counter without PHP/ASP/MySQL just with HTML, Javascript (maybe Ajax?) and flat files?
Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupid[ch8230] and I went ahead

James C4S

#1
Jun 01, 2009, 03:18 pm Last Edit: Jun 01, 2009, 03:18 pm by cmiyc Reason: 1
HTML, AJAX/Javascript are all client side.  You need something server side to do the counting.  You don't need mysql, you but you'll need php, asp, or cgi to store the download count somewhere.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

John_Ryan

#2
Jun 01, 2009, 03:20 pm Last Edit: Jun 01, 2009, 03:21 pm by John_Ryan Reason: 1
I think the closet you'll get is a javascript cookie but that'll only write to a client side file.

A flat file hosted on the server will need a server side language to read and write, like php.

Ajax uses php or asp for server side functions as well, so I don't think there's an Ajax solution either.  

Edit:

As above

mircho

In order to accurately track actual downloads you will have to implement something very simple on the server - some script that just reads a value in a file and increments it. Later display that value by using SSI (server side includes) in your html or use AJAX to load it and place it somewhere on the page.
The setup will be something like that:
in the html have a link to the download
Code: [Select]


<a href="http://server.com/file.zip" id="myDownloadLink">Download file.zip</a>

<p>This file has been downloaded so many times: <span id="myDownloadLinkCounter"></span></p>

<script>
//this is jQuery code that will perform an ajax request to the server
$( function() {



$.ajax( {
  type: "GET",
  url: "theydownloadmyfile.php",
  data: "filename=file.zip",
  success: function(msg){
     $( '#myDownloadLinkCounter' ).text( msg );
  }
} );


$( '#myDownloadLink' ).click( function()
{
$.ajax( {
type: "POST",
  url: "theydownloadmyfile.php",
  data: "filename=file.zip",
  success: function(msg){
  }
} );
return true;
} )
} );
</script>


If it is possible to implement any server side script this will work. I can provide the php for that. If not, than it's irrelevant, but you still can use some of the jQuery code.

Nachtwind

i already thouht it would end up with something like that.. damn limitation about the used languages.. If i was able to use PHP it would be done in seconds, but the host i have to use does neither have any server side scripts nor a database ><
I just thought that javascript was somehow able to do some serverside stuff (like editing a file) as well..
so..
Thanks for your help anyway.
Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupid[ch8230] and I went ahead

mircho

You can use some code similar to what I proposed to "hit" some image counter service. And then display the image in the page.

Nachtwind

hm.. i just begin to think about using my personal server for tracking the hits with your code.. that could be..interestering.
Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupid[ch8230] and I went ahead

mircho

If you have a server at hand and you know php then everything is simpler. Use JSONP ajax requests with jQuery (read here http://api.jquery.com). Basically behind the scenes jQuery creates a script tag with an url on a remote server. When the data is loaded a callback function is invoked. Pretty simple to use.

Go Up