Using a variable in JS replace method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • masterofzen
    New Member
    • Jan 2007
    • 14

    Using a variable in JS replace method

    This seems like such an easy one, but I can't seem to figure it out or find any answers. I think I might be doing something wrong besides the obvious.

    I'm trying to replace a particular variable with a string (specifically, a line break). I'm trying this:

    [code=javascript]
    if (flashsrc.index Of('americancul tist.com') == -1) {
    document.getEle mentById('flash input').replace (embedTag, '\n');
    }
    [/code]
    Is there an obvious reason this isn't working for me? It's kind of a mess right now, but I'm uploading the whole page anyway if you care to peruse it:

    http://www.americancul tist.com/videosource2.ht ml

    What I'm attempting to do here is strip out embed tags that don't come from a certain domain upon form submission. But I don't need a solution to the larger problem for now -- just wondering why this particular step isn't working.
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    You are trying to use a String method on an object.

    Comment

    • masterofzen
      New Member
      • Jan 2007
      • 14

      #3
      Oh, that was my mistake -- just an alternative I was trying. I originally had this:

      [code=javascript]
      if (flashsrc.index Of('americancul tist.com') == -1) {
      field1val.repla ce(embedTag, '\n');
      }
      [/code]

      But that didn't work, either. When I insert an alert afterwards for the value of field1val, it comes up with the variable embedTag unreplaced.

      Comment

      • masterofzen
        New Member
        • Jan 2007
        • 14

        #4
        Figured it out:

        [code=javascript]
        if (flashsrc.index Of('americancul tist.com') == -1) {
        field1val = field1val.repla ce(embedTag, '\n');
        document.getEle mentById('flash input').value = field1val;
        }
        [/code]

        I thought using the replace method would change the string itself, but it doesn't. The string has to be set equal to it.

        Comment

        • beatTheDevil
          New Member
          • Nov 2006
          • 16

          #5
          Originally posted by masterofzen
          I thought using the replace method would change the string itself, but it doesn't. The string has to be set equal to it.
          JavaScript strings are immutable : ) (they can never be changed once constructed)

          Comment

          Working...