Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Using A Windows Batch File To Combine My Js Files

This is a discussion on Using A Windows Batch File To Combine My Js Files, within the Javascript section. This forum and the thread "Using A Windows Batch File To Combine My Js Files" are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Using A Windows Batch File To Combine My Js Files, "different carriage return types" ...er, what?
MikeHopley
post Aug 15 2008, 09:10 AM
Post #1


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


I have two javascript files on my site:
  • jquery.js -- the jquery javascript library
  • java.js -- my own scripts
I keep them separated because java.js is much smaller than jquery.js, so it's easier to work on. Obviously I never edit jquery.js. wink.gif But I want to combine these two files into one (to save an HTTP request). And then I want to minify them using YUI compressor.

This all works fine if I do it by hand: I just copy-&-paste java.js onto the end of jquery.js, and then I run YUI on the resultant file. No problem.

But I want to automate this process, so I wrote a batch file to merge them (replacing the copy-&-paste step). Here's my batch file:

CODE
@echo off
copy %CD%jquery.js + %CD%java.js merged.js


The resultant file looks exactly the same, but when I run YUI it generates (fatal) errors:

CODE
ERROR: [# of my last line of code]: illegal character


Puzzled by this, I loaded up both files in WinMerge to get a side-by-side comparison. WinMerge gave me this message, which I think is important:

QUOTE
These files use different carriage return types.

Do you want to treat all carriage return types as equivalent for this comparison?


When I said "yes", WinMerge showed the files to be identical. When I said "no", Winmerge showed the files to be different on every line.

So what's going on? I presume that my batch file has changed the encoding of carriage returns somehow; but I have no idea how. Help!


** EDIT **

Looking more closely at WinMerge, there is a single character difference at the very end of the file: a strange little box containing "1A".

When I delete this character, YUI works fine.

Looking at the file in Homesite, this single character appears to be a forced line break (it looks like the return/enter key). All the other line breaks are paragraph symbols (the pilcrow).
Go to the top of the page
 
+Quote Post
rewake
post Aug 15 2008, 10:07 AM
Post #2


Rapid Squeezer
Group Icon

Posts: 239
Joined: 14-February 08
From: NY, USA


Hi Mike,

I haven't run into this prob before, but it's definitely a matter of line-break vs carriage return. DOS uses carriage while *NIX uses line break, so I'm thinking YUI is essentially using "\n"s, but when you combine them beforehand with batch it's adding "\r"s to the file.

You could write a bash script to do the same thing, or maybe YUI has a flag for carriage returns? Come to think of it, try remove line breaks and carriage returns from your files before running the batch script... they're useless anyways when you're talking about compressed files.

Or, you could write a PHP script that does this work for you.

Personally, I would leave the two files separate, for a couple reasons...

1. Version control. Every time you update your "java.js" file you will have to merge your files again.
2. Caching. Granted, having separate files means you add an HTTP request for each additional file, but included js files are cached by the browser so they won't be downloaded again.

Hope this helps!
Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
rewake
post Aug 15 2008, 10:09 AM
Post #3


Rapid Squeezer
Group Icon

Posts: 239
Joined: 14-February 08
From: NY, USA


There you go...

1A is hex for "end of file".

Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
MikeHopley
post Aug 15 2008, 10:21 AM
Post #4


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


QUOTE (rewake @ Aug 15 2008, 04:09 PM) *
There you go...

1A is hex for "end of file".

Rich


Aha! Thank you.

So, how can I delete this character automatically, or prevent it from appearing in the first place?

As for the version control/caching: I'm not too worried about that. It's not like I change my javascript every day. I'm planning to use a far-futures expiry together with a version suffix (thereby eliminating HTTP requests altogether once the file has been cached). Obviously this adds a slight development burden, but I don't need to update javascript/css/images frequently.

Besides, this development burden is nothing compared to implementing CSS sprites! wink.gif
Go to the top of the page
 
+Quote Post
MikeHopley
post Aug 15 2008, 10:29 AM
Post #5


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


O-ho! I have solved it.

(Note to Karinne: I didn't solve this one entirely on my own. tongue.gif Rewake's uber-techy input was essential.)

Windows can create files in binary or ASCII mode. So I added the binary switch /b to the output, and it works (creates a binary rather than an ASCII file, I believe):
CODE
@echo off
copy %CD%jquery.js + %CD%java.js merged.js /b
Go to the top of the page
 
+Quote Post
rewake
post Aug 15 2008, 10:40 AM
Post #6


Rapid Squeezer
Group Icon

Posts: 239
Joined: 14-February 08
From: NY, USA


QUOTE
Besides, this development burden is nothing compared to implementing CSS sprites!


Ain't that the truth!! lol I still have a bunch of sprites to create as a matter of fact!

Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
MikeHopley
post Aug 15 2008, 10:54 AM
Post #7


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


QUOTE (rewake @ Aug 15 2008, 04:40 PM) *
Ain't that the truth!! lol I still have a bunch of sprites to create as a matter of fact!

Rich


Have you seen SmartSprites?

I've never used CSS sprites, but I plan to give that a whirl.
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   9 4lun 315 22nd February 2008 - 01:27 PM
Last post by: thesealportalteam
No New Posts   8 Mark 592 10th March 2008 - 04:33 PM
Last post by: Jacob
No New Posts   1 edd 227 27th March 2008 - 01:33 PM
Last post by: edd
No New Posts   3 Mark 170 1st April 2008 - 08:47 AM
Last post by: JustinStudios
No New Posts   10 unitedcraig 300 18th April 2008 - 03:14 PM
Last post by: paintingtheweb