Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: replace()

  1. #1
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default replace()

    Hi, I'm trying to get the replace() method to work for me - it is working, but not exactly how I want it to.

    I want the replace method to run on mouseover, as a substitute for Firefox's content feature, so it will work on different browsers.

    I have it working, but at the moment when I mouseover it's going to a new page, instead of just replacing the text that's already there, here's my code:

    Code:
    <script type="text/javascript">
    function changeText(){
    var str="continue reading";
    document.write(str.replace(/continue reading/, "continue reading..."));
    }
    </script>
    
    <a href="guides-maintenance-f3.html" onmouseover="changeText();">continue reading</a>
    Hope you can help a beginner out,

    Thanks,

    Jack

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Code:
    <script type="text/javascript">
    var el = document.getElementById('guides-maintenance');
    el.onmouseover = function() {
         el.nodeValue = el.replace(/continue reading/, 'continue reading...');
    }
    </script>
    <a href="guides-maintenance-f3.html" id="guides-maintenance">continue reading</a>

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Instant memory leak, just add pants DOM implementation. Also, it's the nodeValue of the text node inside the element that you want, and, at the point where that script runs, the element doesn't yet exist.

    This isn't actually a problem with replace() at all: the problem is your use of document.write(). In fact you don't even need replace():
    Code:
    <a href="guides-maintenance-f3.html" id="guides-maintenance">continue reading</a>
    <script type="text/javascript">
      document.getElementById("guides-maintenance").onmouseover = function() {
        this.firstChild.nodeValue += "...";
      };
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Firstly, thanks very much for the help guys, and using Twey's code works, but 2 problems still need to be sorted.

    I want it to remove the dots on mouseout, and also, the first time you mouse over it, it doesn't work, only on the second mouseover does it start adding dots, hope you can help,

    Thanks,

    Jack

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <a href="guides-maintenance-f3.html" id="guides-maintenance">continue reading</a>
    <script type="text/javascript">
      var e = document.getElementById("guides-maintenance");
    
      e.onmouseover = e.onmouseout = (function(other) {
        return function() {
          var e = this.firstChild,
              v = e.nodeValue;
          e.nodeValue = other;
          other = v;
        };
      })("continue reading...");
    
      e = null;
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Hey, thanks again, but it's now saying "continue reading" on mouseover, and "continue reading...", on mouse out.

    So like that, but just the other way round

    Thanks,

    Jack.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Peculiar, shouldn't do that... but anyway, you can just swap the strings over.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Yea, I've done that, only thing now is it, with it that way, when the page first loads it says "continue reading..." and then after I mouseover once it starts working. Only thing is, I don't want it to say "continue reading..." on page load, know how to do this?

    Thanks,

    Jack.

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I must confess, I'm at a loss as to why such a thing could be occurring. Have you any other scripts on the page that could be causing the issue?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Well, I did put your script inside a function, so I could use it on more than one mouseover event, but I didn't think that would change anything, only other scripts I have on the page is one for a drop down menu from DD, and one I made for tabbed content. =/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •