Forum Moderators: open
1) I am outputting an .xml file that has the line:
<img alt="wigget" src="http://example.com/wigget/red-wigget.jpg" width="100" height="120" class="class1">
Q: How do I use javascript remove all the tags and attributes and change it to:
http://example.com/wigget/red-wigget.jpg
2) On the same .xml file, I have a chunk of text which is stored in a tag from movable type:
...........
<form>
...........
<input type="hidden" name="amount" value="19.99">
</form>
Q: How do I use javascript to extract the 19.99 and replace the entire chunk of text with 19.99?
Thank for all the help rendered!
function replaceNodeWithAttrValue(newValue, refNode) {
var parent = refNode.parentNode;
parent.replaceChild(document.createTextNode(newValue), refNode);
}
<img alt="wigget" src="http://example.com/wigget/red-wigget.jpg" width="100" height="120" class="class1" id="redwigget">
<form id="formX">
<input type="hidden" name="amount" value="19.99">
</form>
window.onload = function() {
var img = document.getElementById('redwigget');
replaceNodeWithAttrValue(img.src,img);
var frm = document.getElementById('formX');
replaceNodeWithAttrValue(frm['amount'].value, frm);
};
Note, I haven't tested any of this, and I'm not sure what context you're running it in (a web page?). Hope it helps though.
Using javascript instead of regex
This title seems strange, why not use a JavaScript regex? The syntax is a little different than whatever you've used, Perl?
Try this, it's untested but should work. If you're familliar with regex's you should recognise the syntax anyway. Note I've changed your double quotes to single for the purpose of containing it within another string.
var text="<img ... src='http://example.com/wigget/red-wigget.jpg' ...>";
text.replace( /^.*src='(.*?)'.*$/, "$1");
alert( text);
You can easily change src to value for the second one, or if you need to use it a lot, use a universal function to extract the parameter you want.
Actually, this script is meant for a Movable Type output and I have found a plugin to write the script in Perl. Would it be easier to write the script in Perl?
Actually, this script is meant for a Movable Type output and I have found a plugin to write the script in Perl. Would it be easier to write the script in Perl?
Second, it may be easier to advise if you gave more information, without telling us what you want as the end result, we can only tell you how to extract the text you want.
You say you're outputting the xml, I assumed you were using AJAX, outputting from what? To where? When?
Is the 'Movable Type' browser/reader thing capable of JavaScript, and does it execute script downloaded after the page has loaded if it's an AJAX style.
You can use Perl, for me it would depend if the page was mainly static or if you needed a lot of processing.
I have a xml file that I want to submit to google base. In the xml file, I want to have this line:
1) <g:image_link>http://www.example.com/image.jpg</g:image_link>
This is done by putting a tag <MT Entry> in the code like this:
2) <g:image_link><MT Entry></g:image_link>
where <MT Entry> = <img alt="One Particular Image" src="http://www.example.com/image.jpg" width="120" height="180" class="left"/>
So, using (2) the xml file displays:
3) <g:image_link><img alt="One Particular Image" src="http://www.example.com/image.jpg" width="120" height="180" class="left"/></g:image_link>
The question is how do I extract out the 'http://www.example.com/image.jpg' and replace the entire link like in (1) above directly by writing codes on the xml file. I think I may be able to use Perl but I'm not sure how to write the code.
JavaScript, again replacing the double quotation marks:
var text = "<g:image_link><img ... src='http://www.example.com/image.jpg' .../></g:image_link>";
text = text.replace( "<img.*?src='(.*?)'.*?>", "$1");
alert( text); And in Perl.....
my $text = "<g:image_link><img ... src='http://www.example.com/image.jpg' .../></g:image_link>";
$text =~ s/<img.*?src='(.*?)'.*?>/$1/;
print "$text\n"; If you're not already well versed in Perl I'll help you if you post in that forum.