Yet-another issue with replace in javascript :(

Discussion in 'JavaScript' started by publicidadpixelada, Feb 11, 2007.

  1. #1
    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 [I]oldurl[/I] = document.getElementById('link');
    var [I]newurl[/I] = oldurl.replace("http://oldurl.com", "http://newurl.com");
    document.write('<iframe src="' + [I]newurl[/I] + '" width="500" height="500"></iframe>');
    </script>
    Code (markup):
    But the script get stuck in the replace part and I keep getting the following error in Mozilla:
    oldurl.replace is not a function
    Code (markup):
    And in IE:
    Object doesn't support this property or method
    Code (markup):
    Any ideas? Please it's driving me crazy!
     
    publicidadpixelada, Feb 11, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    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.
     
    phper, Feb 11, 2007 IP
  3. publicidadpixelada

    publicidadpixelada Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but if the anchor is not the href attribute.,, Is there anyway other way to replace it?

    Thanks in advanced!
     
    publicidadpixelada, Feb 11, 2007 IP
  4. webisfun

    webisfun Peon

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    webisfun, Feb 11, 2007 IP
  5. publicidadpixelada

    publicidadpixelada Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks a lot... Just what I was looking for
     
    publicidadpixelada, Feb 11, 2007 IP