<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
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
>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