Forum Moderators: open
The string is generated content from cart software.
The string is always a dollar amount, (e.g. start with a dollar sign and contain a period).
It is always found in the HTML as:
<font class="dogs cat_dog">STRING_DOLLAR_VALUE</font>
It is in the 3rd <font></font> tag pair on the page.
I need to write the STRING_DOLLAR_VALUE somewhere else on the page, and am having trouble pulling it out.
Here's some of what I've tried:
var fontTags = new Array();
var fontTags = document.getElementsByTagName('font');
for (n=0; n<fontTags.length; n++){
document.write(fontTags[n].innerHTML + "<br />");
}
The problem: It reads the first font element it finds, but then dies, I need the value of fontTags[2].
I can't tell if it dies because the dollar sign or period is messing it up, (a string / quoting / interpolation problem), or if I'm missing something else.
Javascript is on my list of skills right after "eats without spilling food on self" -- and I don't do well at that one either.
Any clue to what I'm doing wrong?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Test </title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body><font class="dogs cat_dog">$99.99</font>
<font class="dogs cat_dog">$199.99</font>
<font class="dogs cat_dog">$299.99</font>
<font class="dogs cat_dog">$399.99</font>
<font class="dogs cat_dog">$499.99</font>
<script type="text/javascript">
var fontTags = new Array();var fontTags = document.getElementsByTagName('font');
for (n=0; n<fontTags.length; n++){
document.write(fontTags[n].innerHTML + "<br>");
}
</script></body>
</html>
However, not knowing exactly what it is you are trying to accomplish, this is probably not the best method to be using. In fact, I don't think font tags should be used at all.
I didn't get a solution, but I got the answer.
It's not finding the string I want because the JS is being called at a point in the document before the string is written out.
(The alert method never shows it -- it displays the first occurrence of the <font></font> tag and displays the innerHTML string, but never finds the other two).
The situation is; I need to fix a layout problem in a customer's cart using proprietary software. I don't have access to the source, and the only point in the software I can insert the JS is before the tag sequence appears, (either in the HEAD of the page or in an unused (text) field.
I am going to look for another place in the software to insert the JS.. I may be able to insert it into the template (footer section) with a conditional statement to only execute the JS if the document URL matches the section(s) of the site I want to affect, (the template is used site-wide, e.g.- page may be named ThisPage.asp, ThatPage.asp, or OtherPage.asp, and I only want to manipulate the appearance of "ThatPage.asp").
I've already got the "if" worked out -- now I just need to get the right DOM nodes and tags worked out so it executes and inserts in the right sequence.
Thanks. Will post back the solution when it works.
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript">
YAHOO.util.Event.onDOMReady(function () {
// Your code here
});
</script>
The solution was to break the JS in two... I used the first part to place a DIV tag with an ID in the code at the point where I wanted to replace the content (I couldn't replace the code yet, I needed to read part of it from a latter portion of the generated HTML):
<div id="rpd"></div>
I then placed more JS in the footer of the site's template, (I've reduced it down to the relevant code here):
var fontTags = new Array();
var fontTags = document.getElementsByTagName('font');
And once it collected the info I needed, I used
document.getElementById('rpd').innerHTML = '<span style=\"color:red;\">' + fontTags[2].innerHTML + '</span><br /';
I needed to do more than color the text red, (I just used that as an example here of how to manipulate the original string -- I really needed to move it from it's original position in the HTML along with some text, graphics, links and images -- but it all worked the same way).
Done and done! It ended up being a two-step-shuffle, with an innerHTML replace that got fed another innerHTML replacement, but it was fun!
Thanks to all.