Creative Electronics - Debt Consolidation - Debt Consolidation - Find jobs - Find jobs

PDA

View Full Version : Yet-another issue with replace in javascript :(


publicidadpixelada
Feb 11th 2007, 3:39 pm
Hey... Just a quick question regarding the replace method in javascript.

I have the following html document to replace an url (oldurl) for another url (newurl):
<a id="link" href="http://oldurl.com">Old Url</a>
<script type="text/javascript">
var oldurl = document.getElementById('link');
var newurl = oldurl.replace("http://oldurl.com", "http://newurl.com");
document.write('<iframe src="' + newurl + '" width="500" height="500"></iframe>');
</script>
But the script get stuck in the replace part and I keep getting the following error in Mozilla:
oldurl.replace is not a function
And in IE:
Object doesn't support this property or method

Any ideas? Please it's driving me crazy!

phper
Feb 11th 2007, 4:08 pm
Hi there,
'oldurl' is a DOM object, not a string. Thus the error message.
You may want to get the 'href' (assuming it's an anchor) and perform a replace on it.

Also, the first parameter for 'replace()' should be a regular expression.

publicidadpixelada
Feb 11th 2007, 5:14 pm
Hi there,
'oldurl' is a DOM object, not a string. Thus the error message.
You may want to get the 'href' (assuming it's an anchor) and perform a replace on it.

Also, the first parameter for 'replace()' should be a regular expression.

Thanks, but if the anchor is not the href attribute.,, Is there anyway other way to replace it?

Thanks in advanced!

webisfun
Feb 11th 2007, 5:44 pm
I think that should do it

oldurl.href.replace(

to keep it simple, the href element is a string, so now it has the method you are looking for.

publicidadpixelada
Feb 11th 2007, 9:59 pm
I think that should do it

oldurl.href.replace(

to keep it simple, the href element is a string, so now it has the method you are looking for.

Thanks a lot... Just what I was looking for