dslreports logo
    All Forums Hot Topics Gallery


how-to block ads

Search Topic:
share rss forum feed


Overhere, HI

Share code

Time to share! Let's make this a thread where we post code we made ourselves - Useful and nifty code snippets in any language! :)

I'll start off with this D/L Speed Meter thingy I just made. I thought it would suite the DSLR "audience". :)

It displays a discrete line at the bottom of the web page that says "Page loaded in X.XX seconds at YY.YY KB/s" (where X and Y are calculated every time the page loads). You can change anything about the code to suite your needs if you like, or just "cut-paste-andforgetaboutit". The script is compatible with all >4 browsers.

The only thing you need to change in the script is the variable "fsize". Set it to the total amount in KB of the webpage containing the code and its content(e.g. one 10KB .html page + two 5KB images: fsize = 20).

Put this in between the HEAD tags:

<script language="JavaScript" TYPE="text/javascript">
Start = new Date()
function dlTime() {
var fsize = 20; //Total size in KB of the html page + images etc
var Diff;
Stop = new Date()
Diff = (Stop.getTime() - Start.getTime())/1000;
var kbs = fsize/Diff;
function roundOff(x,dec) {
return (Math.round(x*Math.pow(10,dec)))/Math.pow(10,dec)
var fstring = ""+"Page "+"loaded "+"in "+Diff+" seconds "+"at "+roundOff(kbs,2)+" KB/s";
if (document.layers) {
else if (document.all) {
loadtime.innerHTML = fstring;
else if (document.getElementById&&!document.all) {
document.getElementById("loadtime").innerHTML = fstring;

Put this anywhere between the BODY tags:

<div ID="bottom" STYLE="position:absolute; width:300px; height:10px; z-index:99;
left: 37%; top: 97%"><font face="Arial,sans-serif" color="#BBBBBB" size="-3">
<span ID="loadtime" STYLE="position:relative; width:300px; height:10px;"></span></font></div>

And finally add the onLoad event handler to the body tag:

<body onLoad="dlTime()">

One thing about what the "meter" shows: If you allow the page to be cached, the meter will only show the correct speed the first time the page loads of course(in between clearing of TIF). After that it will just measure how fast it loads from cache. So if you want it to show the correct download speed every time the page loads, and not just the "load time", I suggest you use cache-control and set the content expiration date to "-1". Which ever case; it's a nifty thing to have on the page(s). :)

Pontiac Freak
Looking for 1971-1976 GM Station Wagons
Mesa, AZ
Very Nice! Thanks PingPong

My socks don't match.
Hawaiian Jellyfish
Mile High
Here's my contribution: »Server load in PHP

It's in PHP and MySQL and determines server load.
"You can't truly understand something until you can take it out of context."


Vernon Hills, IL
reply to PingPong3
Well, hopefully this will sneak in as a useful code-esque tip.

In Flash, if you find yourself creating buttons like mad with images in them that you imported or drew, there is an easier way. I found that it is easier to just make part of the movie clickable and to heck with swapping images out and about.

How to do it:
Create a button, on the up, over, and down keyframes, leave them blank. On the hit keyframe, draw a rectangle or square or what have you. When the time comes to put it in the movie, just drag it out and scale away.

This is great if you are doing multiple choice questions, like a quiz, or if you are doing system simulations with screen grabs.
"Out beyond fields of wrongdoing and rightdoing, there is a field. I will meet you there." Jelaluddin Rumi

Everybody Wang-Chung Tonight

Nearest Bar
reply to PingPong3
Hardly groundbreaking but I am at work and this is the first thing that came to me, anything placed in backticks - thats the ` symbol on the same key as the tilde - within PHP (and other languages) will be executed as a command on the server. This can be used for several things, but I think it is cool with one line of php code you can have a different fortune displayed each time.
This assumes you are using a *nix server and Fortune is in the normal place. Insert brackets as appropriate.

echo htmlentities (`/usr/games/fortune`);

The htmlentities command just makes sure the output is converted to valid html, it works without it, but it is a better idea to leave it there.
As I said hardly groundbreaking, but I figured it out while playing with something else, and always thought it was a nice simple way to spice up an otherwise static page, without having to do any real coding. You can see it in action at »www.indiemonkey.com/limeygit - which is just a testing page and probably shouldn't be viewed by those on Netscape 4.x
Taking you to Funky Town since 1999!


Lawrenceville, GA
reply to PingPong3
Thanks for posting. I love to read other peoples code. I get bored staring at my own programming style all the time :). I just finished my first "real" program in Perl, and this is what I used to process the forms it had to handle. It's nothing special. In fact it could probably be written alot better, but I'm no Perl hacker. I also have a bad habit at using non-descriptive variable names which is evident in this snippet.

sub getForm
for($loop = 0;$loop != $ENV{'CONTENT_LENGTH'};$loop++)
$data =~ s/%2F/\//;
$data =~ s/%3A/:/;
@temparray = (split(/&/,$data));
$temp = @temparray;
%temphash = ();
for($loop = 0;$loop != $temp;$loop++)
($name,$data) = split(/=/,@temparray[$loop]);
$temphash{$name} = ($data);
return %temphash;

It was designed to return all the data to a hash. It also only handles forms submitted using POST. Personally I don't think it's well written. It just seems bloated and ineficient. Maybe I'm just too critical of my own code...
I'm not paranoid: everyone IS out to get me.

Prolific Bunny
Premium,ExMod 2001-04
Bordentown, NJ
reply to PingPong3
Very nice you web monks.

Drexel Hill, PA
reply to PingPong3

And I celebrate when I figure out how to do a 'user registration via email link' on my website.

When you learn as you go.. you take what you can get

Curiosity MAY have killed Schrodinger's cat.


Per Ardua Ad Astra
ExMod 2002-04
downloadantispamscript.zip 848 bytes
Spam-Proof Email Linking

Recently I've been seeing more and more spam arrive on certain mail-accounts we are using here at home. Especially the account of my brother that is mentioned on a certain website.

Most of you have also probably seen the attempts made by Justin to project the e-mail address that are on some of our DSLR profile pages (if you opted in on your preferences page)

Nice image Justin, but I can't click on it

So what did I do? Combine the best of both worlds I hope

I decided to share it; see the attached file for more since I cannot add the code in this post without messing it up
(even when using the {code} and {pre} block

You can of course change the image to whatever you like and I think this is very cool

I really hope you like this solution!
~ DSLR members save >2,6 Mln. Sq. Ft. of nature! Join us, it's free! ~

Miami, FL
This was such a neat and useful script I decided to make it visible on the page for you, Starfish. Good work.

<title>Spam-Proof Email Linking</title>


<script language="JavaScript">
function mailme()
This script will try to prevent that your e-mailaddress is being found by spambots.
You can easily configure it to suit your own needs:

- 'var user' contains your complete username that comes before the '@'-sign, REQUIRED!

- 'var domain' contains the complete domain that comes after the '@'-sign, REQUIRED!

- 'var subject' contains a subject and is OPTIONAL. Just don't remove or comment the line,
because the script WILL return an error. Just make it blank if you don't need it
(var subject = "")

Made by Starfish, 2002
var user = "thestarfish";
var domain = "gmx.net";
var subject = "About your Spam-proof Script.."
window.location = ('mailto:' + user + '@' + domain + '?subject=' + subject);

<a href="#" onClick="mailme()"><img src="http://www.dslreports.com/i/button.php3?font=eurostib&bg=255255255&fg=000000000&sh=128128128&delta=0&sz=16&text=thesta rfish@gmx.net" border="0"></a>


I have not failed 10,000 times. I have successfully found 10,000 ways that will not work.
- T.A. Edison

Prolific Bunny
Premium,ExMod 2001-04
Bordentown, NJ
reply to PingPong3
Excellent posts guys... Perhaps tomorrow I can start re-organizing the forum's header.