Computing.Net > Forums > Programming > Can't insert Javascript/CSS string into HTML

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Can't insert Javascript/CSS string into HTML

Reply to Message Icon

Name: mariachimike
Date: September 21, 2009 at 01:18:31 Pacific
OS: Windows XP
Product: Microsoft Windows xp professional edition
Subcategory: Batch
Comment:

I'm trying to write a build script that will:
1. Minify JS and CSS files
2. Read them into variables
3. Insert those strings into HTML

The first 2 steps are successful, but I am unable to successfully insert the strings. I've found that if I just try to insert a string with no spaces or special characters, it will work. But once I try to insert the JS or CSS strings, it seems they are being expanded during the call and then parsed.

Here is my code:

SETLOCAL EnableDelayedExpansion

:: Minify JS
java -jar yuicompressor-2.4.2.jar --type js --nomunge "%~dp0src\main.js" > temp1.txt
SET /p MAINJS= < temp1.txt

:: Minify CSS
java -jar yuicompressor-2.4.2.jar --type css "%~dp0src\main.css" > temp3.txt
SET /p MAINCSS= < temp3.txt

:: Insert Strings
BatchSubstitute.bat ^<!--MAINCSS--^> "%MAINCSS%" "%~dp0src\main.html" > temp.txt

ENDLOCAL

Here is the BatchSubstitute.bat code:
(from http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplace)

@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
if "%*"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)

I've tried wrapping %MAINCSS% with no quotes, 1 quote, 2 quotes and all fail. No quotes and 2 quotes fail with "System can't find specified file" and 1 quote fails with "body shouldn't be used at this time" (translated from Chinese).

Can anybody point me in the right direction here? Any help would be appreciate, thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: mariachimike
Date: September 29, 2009 at 01:03:09 Pacific
Reply:

Well, in the end, somebody told me about Windows Scripting host and ended up using Javascript to write the string insertion part.


0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Can't insert Javascript/CSS string into HTML

Can't open with Frontpage www.computing.net/answers/programming/cant-open-with-frontpage/8771.html

how to combine two strings into one www.computing.net/answers/programming/how-to-combine-two-strings-into-one/800.html

dumb q about html www.computing.net/answers/programming/dumb-q-about-html/8201.html