Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Script To Calculate/report Html Document Filesize
nachtgeist03
post Apr 21 2005, 08:04 AM
Post #1


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 59
Joined: 16-April 05
Member No.: 4,039



Looking for a script, and not sure what sort of script I would need, that simply will post a file size in a body of text. Example:

CODE
Please note: The page you are trying to view is 90KB. Loading of this page may take longer than normal, even on good connections.


Where the "90KB" part would automatically change depending on the file size. And if it could calculate the approximate load time on a 56k connection, that would be an added bonus (however, not required).

This post has been edited by microscopic^earthling: Apr 21 2005, 08:55 AM
Go to the top of the page
 
+Quote Post
dungsport
post Apr 21 2005, 08:49 AM
Post #2


Member - Active Contributor
Group Icon

Group: Members
Posts: 93
Joined: 21-March 05
Member No.: 3,136



Prerequisite: server-side scripts like php, java, asp...

You can write some PHP code like this
CODE
<?php
// outputs e.g.  'funnies.html': 1024 bytes
$filename = 'funnies.html';
echo  'Please note: The page you are trying to view is ' . filesize($filename) . 'bytes. Loading of this page may take longer than normal, even on good connections.'
?>

OR
CODE
<?php
// outputs e.g.  'funnies.html': 1 KB
$filename = 'funnies.html';
echo  'Please note: The page you are trying to view is ' . (filesize($filename)/1024) . 'KB. Loading of this page may take longer than normal, even on good connections.'
?>

More information here: http://au.php.net/manual/en/function.filesize.php

Or using java like this:
CODE
File file = new File("infilename");

// Get the number of bytes in the file
long length = file.length();
System.out.println("Please note: The page you are trying to view is " + (length/1024) + "KB. Loading of this page may take longer than normal, even on good connections."):

More information of File class could be seen here: http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

Post it here if you have any further question.
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Apr 21 2005, 08:51 AM
Post #3


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411
myCENTs:84.36



    [/tab]First of all this feature won't work on any browsers except IE 4+. Besides, if you're using IE, this can easily be done with an ActiveX object. The javascript code, however, won't report size of any external files that you include in your HTML doc, like StyleSheet files and all. Anyways, here's the JavaScript:

CODE

function getfilesize()
{
if (!document.fileSize)
{
 alert('This script does not work in browsers except IE 4+.');
 return;
}

var size_of_file = (document.fileSize) * 1;

       document.write (size_of_file);
}



[tab]A little explanation is in order I believe.
CODE

if (!document.fileSize)
{
 alert('Filesize reporting does not work in browsers except IE 4+.');
 return;
}

This piece of code check whether the document.fileSize function reports any value back. Since this isn't available except on IE 4+, all other browsers will report false and hence you'll be shown an error message.

Next is:
CODE

var size_of_file = (document.fileSize) * 1;

This is quite simple - the only trick is in multuplying the returned value with 1. The filesize reports in the form of a string and hence it has to be multiplied with ONE to convert it into a numerical value.

The last part:
CODE

       document.write (size_of_file);

This just writes out the value to your HTML doc.

Combine this with your HTML Message to get the desired effect.
CODE

Please note: The page you are trying to view is <script>getfileSize</script> Bytes. Loading of this page may take longer than normal, even on good connections.


Keep in mind that the filesize is reported in bytes - in order to produce a KB display, simply add a division by 1024 in the function.

Hope this helps.
Go to the top of the page
 
+Quote Post
dungsport
post Apr 21 2005, 08:58 AM
Post #4


Member - Active Contributor
Group Icon

Group: Members
Posts: 93
Joined: 21-March 05
Member No.: 3,136



@m^e, as I understand, what he is asking is not about the file size of the current page. It is the size of the file they are going to open to see. If they know it is to big and too slow to download then they would not click on it.

We should give this information to visitors before hand.
Go to the top of the page
 
+Quote Post
nachtgeist03
post Apr 21 2005, 09:01 AM
Post #5


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 59
Joined: 16-April 05
Member No.: 4,039



Would there be a way to write that into an applet, so that at the bottom of the page, just a small area would report the file size, and if there's any problem with the applet, it's just a small grey box?

Or is there another way to do it where I'm not limited to IE-only? I know more and more people are leaving IE in favor of other browsers (firefox, opera, etc).

Please combine this with my previous post as I haven't an idea as how to edit that post.

But dungsport is correct. The page is just a large table, but by large, I'm saying, almost 2,000 fields in the table presenting data (around 270 rows, 7 columns), and I don't want someone to just click on it thinking it to be a quick loading page. But I also don't feel like changing the page with the warning every time I update the big table.

Notice from vizskywalker:
merged on nachtgeist03's request
Go to the top of the page
 
+Quote Post
vizskywalker
post Apr 22 2005, 04:55 AM
Post #6


Techno-Necromancer
Group Icon

Group: Members
Posts: 1,018
Joined: 13-January 05
From: The Net
Member No.: 2,127



The php script provided by dungsport should work with all browsers because it is done server-side. M^E was referring to the javascript when he said it was limited to IE4+ because javascript parsing is performed by the browser. To use the php script, simply copy and paste it into your html document and change the extension to .php.

~Viz
Go to the top of the page
 
+Quote Post
marijnnn
post Apr 22 2005, 02:16 PM
Post #7


Premium Member
Group Icon

Group: [HOSTED]
Posts: 336
Joined: 22-September 04
Member No.: 798



yeah, that script is fine. off course it works on all browsers. serverside scripts always do! anyone telling you different is selling you crud.
so an easy script to give you a list of all the files in one directory would be something like this:
CODE

<?php
define("DIRECTORY","./files/");
$dir = opendir(DIRECTORY);
while (false !== ($file = readdir($dir))) {
      echo "<LI><A HREF='".DIRECTORY.$file"'>$file</a> filesize: ".filesize($file)."</LI>";
  }



should do the trick. haven't tested it, might have errors.
off course, you can replace DIRECTORY with $_GET["dir] or something like that.

This post has been edited by vizskywalker: Apr 22 2005, 07:53 PM
Go to the top of the page
 
+Quote Post
Hercco
post Apr 27 2005, 05:55 PM
Post #8


Super Member
Group Icon

Group: Members
Posts: 595
Joined: 4-September 04
Member No.: 228



To make the script actually useful it'd be nice that it could calculate the total size that the browser has to load to view the whole page.

The examples above simply give the filesize of the html file at the server, not any included files, like javascripts, applets, images, CSS-sheets etc...

But it wouldn't be too hard to do. First thing to decide is what objects to include to the size. Then write a parser taking in account all the required files. Simply seek out all the "<img src=" or "src" inside "<link" take the filenames and pass them to filesize and add the output to the total size.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Converting HTML over to XHTML(13)
  2. How To Make A Text Based Online Game Script ?(24)
  3. PHP: Writing A Generic Login And Register Script(15)
  4. Bash Script To Display Your Ip(9)
  5. Zoho Writer(5)
  6. Running Vba Script In Excel(7)
  7. Converting PSD To HTML(11)
  8. Office 2007 Document Problems(14)
  9. Need A Javascript To Enable / Disable Buttons(2)
  10. Html Basic Tutorial(9)
  11. Auto-click Script(7)
  12. Myspace Gold Script(2)
  13. Looking For Script(5)
  14. Palestinian Power(3)
  15. How To: Display A Members/user List.(3)
  1. Web Editor(0)
  2. Indentation In Html(4)
  3. Joomla Template Kit Extension For Nvu/composer(3)
  4. What You Need Before You Can Create A Text-based Game..(7)
  5. Basic Html Tutorial(1)
  6. Automatic Typing Script(3)
  7. Good Books For Html And Css Beginners(1)
  8. [fl]snow Effect(4)
  9. Ffow: Frontlines: Fuel Of War(2)
  10. How To Validate The Login Form Using Php Pcre(0)
  11. How To Design The Popup Menu Item Using Javascript(1)(0)
  12. [report] A User(2)
  13. Extracting "attributed" Data From An Xml Document/data-island(0)


 



- Lo-Fi Version Time is now: 5th December 2008 - 12:30 PM