I am far from a PHP expert, and I've been struggling to create a function
which will take a javascript .js file and "compact" it as much as possible.
Meaning, remove all comments and unnecessary whitespace. "Obfuscatio n" is
not necessary. Obviously, the compacted javascript should run identically to
the original javascript.
Can anyone point me to an existing function that does this?
Thanks!
--
Matt Kruse http://www.JavascriptToolbox.com http://www.AjaxToolbox.com 17 2116
Matt Kruse wrote: I am far from a PHP expert, and I've been struggling to create a function which will take a javascript .js file and "compact" it as much as possible. Meaning, remove all comments and unnecessary whitespace. "Obfuscatio n" is not necessary. Obviously, the compacted javascript should run identically to the original javascript.
Can anyone point me to an existing function that does this?
No, but if you're the Matt Kruse I'm thinking of, you know how to program in
Javascript. Given the similarity in syntax, you may find the PHP5 function
php_strip_white space() meets your requirements. If not (or if you're
running an earlier version) then...
Regular expressions can be used for all of this in PHP, although I'd
probably use str_replce for most of the whitespace stuff:
$js=str_replace ("\t", ' ', $js); // tab chars
$js=str_replace ("\r", '', $js); // strip CR
$js=str_replace ("\n\n", "\n", $js); // double NL
$js=str_replace (' ', ' ', $js); // double space -> single space
I'd also strip a space following an operator/bracket/quote but
that needs something smarter - hence regexes - these need a bit
more thought put into them. ISR the question about stripping comments
coming up in the PHP newsroups several times before - try Google.
HTH
C.
Matt Kruse wrote: I am far from a PHP expert, and I've been struggling to create a function which will take a javascript .js file and "compact" it as much as possible. Meaning, remove all comments and unnecessary whitespace. "Obfuscatio n" is not necessary. Obviously, the compacted javascript should run identically to the original javascript.
Can anyone point me to an existing function that does this?
Thanks!
<URL:http://www.crockford.c om/javascript/jsmin.html>
" JSMin is a filter which removes comments and unnecessary whitespace
from JavaScript files. It typically reduces filesize by half, resulting
in faster downloads. It also encourages a more expressive programming
style because it eliminates the download cost of clean, literate
self-documentation. "
Obfuscation is not performed, uglification is. ;-)
--
Rob
JDS wrote: I can't. But a beter question is, why bother? Is your javascript stuff really so huge that taking out the whitespace will matter that much?
Whitespace reduction often isn't as dramatic as comment removal, but with
both combined - yes.
If comments are used liberally and documentation for a library is included
within the source file itself, a js file could be, say, 50k or more.
When compacted, the same file could be 25k or even less. Since it has the
same functionality, there's no reason _not_ to compact it and save some
bandwidth and download time.
--
Matt Kruse http://www.JavascriptToolbox.com http://www.AjaxToolbox.com
Colin McKinnon <co************ **********@ntlw orld.deletemeun lessURaBot.com>
wrote: Matt Kruse wrote:
I am far from a PHP expert, and I've been struggling to create a function which will take a javascript .js file and "compact" it as much as possible. Meaning, remove all comments and unnecessary whitespace. "Obfuscatio n" is not necessary. Obviously, the compacted javascript should run identically to the original javascript.
Can anyone point me to an existing function that does this? ... Regular expressions can be used for all of this in PHP, although I'd probably use str_replce for most of the whitespace stuff:
$js=str_replac e("\t", ' ', $js); // tab chars $js=str_replac e("\r", '', $js); // strip CR $js=str_replac e("\n\n", "\n", $js); // double NL $js=str_replac e(' ', ' ', $js); // double space -> single space
It isn't that easy. This won't strip // type comments, and it will screw
up all of the string constants. It isn't rocket science, but you basically
need to implement a miniature Javascript parser to make this work reliably.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Matt Kruse wrote: RobG wrote:
URL:http://www.crockford.com/javascript/jsmin.html
No version for Mac ? :-(
1) It ain't PHP. I want PHP :)
I do not know PHP but I heard something about gzip ...
if your server has the good library
and if you accept files *.php
<?php ob_start('ob_gz handler'); ?>
<!DOCTYPE HTML PUBLIC
blah blah and rest of page
</html>
would compress file on fly via gzip
(I've seen gain : up to 90% if no image)
You can aslo compress your files at home with gZip (*.gz)
and then upload them on your server
Every browser (even NC3)
can decompress this datas on fly in few mili-seconds
--
Stephane Moriaux et son [moins] vieux Mac
"Matt Kruse" <ne********@mat tkruse.com> wrote: RobG wrote: URL:http://www.crockford.com/javascript/jsmin.html
1) It ain't PHP. I want PHP :)
Convert it from C to PHP :-)
But if you can compile C on your server, I would not bother and just
use system() to call it. http://jscompact.sourceforge.net/ is an alternative for jsmin that
uses spidermonkey. Using a real JS engine, it should be even
better.
Bye,
Martin
Matt Kruse said the following on 8/14/2005 10:32 PM: JDS wrote:
I can't. But a beter question is, why bother? Is your javascript stuff really so huge that taking out the whitespace will matter that much?
Whitespace reduction often isn't as dramatic as comment removal, but with both combined - yes.
If comments are used liberally and documentation for a library is included within the source file itself, a js file could be, say, 50k or more.
When compacted, the same file could be 25k or even less. Since it has the same functionality, there's no reason _not_ to compact it and save some bandwidth and download time.
Read the file in with PHP.
Split it on line boundaries (if it doesn't already) so that you end up
with an array that is the same length as the number of lines of code.
Loop through the array and remove any empty entries (blank lines in the
code), entries that start with \\.
Then you would have to write a loop that would find comments that start
with \* and find the next entry that ended in *\ and remove them.
Should remove all the comments (unless they are inline comments :) )
var k = "My mama"; //This var keeps track of my Mama
Sounds like fun to try to write it but it's been a while for PHP, might
give me a good reason to freshen up on it. If not, write it in JS and
let you convert it to PHP :)
--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Cardman |
last post by:
Greetings,
I am trying to solve a problem that has been inflicting my self
created Order Forms for a long time, where the problem is that as I
cannot reproduce this error myself, then it is difficult to know what
is going on.
One of these Order Forms you can see here...
http://www.cardman.co.uk/orderform.php3
|
by: cheezebeetle |
last post by:
ok, so I am having problems passing in an ASPX function into the Javascript in the codebehind page. I am simply using a confirm call which when they press "OK" they call this ASPX function, when they press "Cancel" they call another ASPX function.
My code now is:
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
System.Web.HttpContext.Current.Response.Write("if (confirm('Are you sure you want to...
|
by: Matt Kruse |
last post by:
I am far from a PHP expert, and I've been struggling to create a function
which will take a javascript .js file and "compact" it as much as possible.
Meaning, remove all comments and unnecessary whitespace. "Obfuscation" is
not necessary. Obviously, the compacted javascript should run identically to
the original javascript.
Can anyone point me to an existing function that does this?
Thanks!
|
by: Water Cooler v2 |
last post by:
http://www.w3schools.com/js/js_whereto.asp
This link is to a JavaScript tutorial on w3schools. The page says that
a script put in the HEAD is executed only when called, whereas one put
in the BODY is executed as the page loads (implying invariably, whether
called or not).
To test, I tried the following two pages:
|
by: mistral |
last post by:
How to compile javascript code in form of ActiveX control? This will
make code more compact and easier to manage on web page. I.e. part of
my javascript code already contains activex objects, and i just want
embed it into more compact form, like <object
classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ...
m.
| |
by: ajos |
last post by:
hi frnds,
i have a form,which has 2 input text boxes, the values are entering the text boxes,when i leave the 2 text boxes blank and hit submit a java script gives the message that the 2 fields are blank.....the problem now is when i leave the text boxes blank the message appears...and when i click ok...then instead of showing me the page its giving me an exception-->
type Exception report
message
description The server encountered an...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |