Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

template slash problem

0 views
Skip to first unread message

e_to_the_x

unread,
Feb 23, 2002, 12:15:05 AM2/23/02
to
I'm the epitome of the JavaScript newbie, and have a problem. Some
software I use (Media Jukebox, to be exact), uses HTML templates for
one of it's features. You write a perfectly normal HTML document, and
it parses it, replacing keywords with its own text, and finally
displaying it. Example:

<html>
<body>
<img border="0" src="TRACKINFO_INSERT_IMAGE" width="450" height="450">
</body>
</html>

TRACKINFO_INSERT_IMAGE is the keyword. I want to insert code so that
the image is only displayed if the replacement text doesn't contain
the text ".png" (I'm using !/.png/.test(TRACKINFO_INSERT_IMAGE) now).
The problem is that the replacement text contains several
Windows-style slashs ('\'), and I'm guessing JavaScript is
interpreting these as escape charecters or something. The filepath the
browser gets is condensed trash.

I really don't know much by way of how JavaScript actually outputs
it's data to the browser or HTML file, and any attempts to remove the
keyword from the <script> tags ends up causing the filepath to include
the actual code.

Any info or ideas would be appreciated. Thanks.

Josh

Mark McVicker

unread,
Feb 24, 2002, 4:01:20 AM2/24/02
to
I didn't test my code w/ Media Jukebox, but I found two things that
might be of use to you.

First, if memory serves, a period in regexps means ``any single
character except newline (\n)''. So for a literal period escape it:
/\.png/

Next, RegExp.test() returns a boolean value (true/false). So, the code
I tested (using the url of the first link on the page as my test string)
looks something like:

if(!/\.png/.test(document.links[0].value) == true) {
alert('true');
}
else { alert('false'); }

If necessary, you can account for .png or .PNG or whatever:

//when page completes loading:
window.onload = function() {
//case-insensitive search
var pattern = /\.png/i;
//the string to check
var s = TRACKINFO_INSERT_IMAGE;

if(pattern.test(s) == false) {
/* insert your code here */
}
}//close function

Hope this helps.

Cheers,
-Mark

Mark McVicker

unread,
Feb 24, 2002, 4:13:47 AM2/24/02
to
whoops. It seems like I gave you bad code. I'll have to reevaluate.
-M

Steve van Dongen

unread,
Feb 25, 2002, 5:16:23 PM2/25/02
to
On 22 Feb 2002 21:15:05 -0800, mccart...@yahoo.com (e_to_the_x)
wrote:

>I'm the epitome of the JavaScript newbie, and have a problem. Some
>software I use (Media Jukebox, to be exact), uses HTML templates for
>one of it's features. You write a perfectly normal HTML document, and
>it parses it, replacing keywords with its own text, and finally
>displaying it. Example:
>
><html>
><body>
><img border="0" src="TRACKINFO_INSERT_IMAGE" width="450" height="450">
></body>
></html>
>
>TRACKINFO_INSERT_IMAGE is the keyword. I want to insert code so that
>the image is only displayed if the replacement text doesn't contain
>the text ".png" (I'm using !/.png/.test(TRACKINFO_INSERT_IMAGE) now).
>The problem is that the replacement text contains several
>Windows-style slashs ('\'), and I'm guessing JavaScript is
>interpreting these as escape charecters or something. The filepath the
>browser gets is condensed trash.

Yes, \ is the escape character.

>I really don't know much by way of how JavaScript actually outputs
>it's data to the browser or HTML file, and any attempts to remove the
>keyword from the <script> tags ends up causing the filepath to include
>the actual code.

I'm not sure what that means. Maybe something related to Media
Jukebox.

>Any info or ideas would be appreciated. Thanks.

Put quotes around the keyword so that you'll end up with a string
after the keyword is replaced. The fact that paths are all messed up
because the \ is not escaped properly is irrelevant because you only
care about the last 4 characters. Change the regexp to be case
insensitive, escape the . because otherwise it matches any char, and
add the EOL character ($).

!/\.png$/i.test("TRACKINFO_INSERT_IMAGE")

Try this:

<html>
<body>
<script type="text/javascript">
if (!/\.png$/i.test("TRACKINFO_INSERT_IMAGE"))
document.write('<img border="0" src="TRACKINFO_INSERT_IMAGE"
width="450" height="450">'
</script>
</body>
</html>

Regards,
Steve

0 new messages