Display word count for this script

Discussion in 'JavaScript' started by mark_s, Jan 11, 2008.

  1. #1
    Is it possible to have the word count displayed in the link that shows the hidden text? This website uses it quite a lot for their show/hide script, scroll down and you'll see.

    PS. Sorry for creating another thread on this script but this should be the last addition I need.

    Demo: http://www.murraysworld.com/test.php

    Javascript,
    <!--
    function dsp(loc){
       if(document.getElementById){
          var foc=loc.firstChild;
    
             foc=loc.firstChild.innerHTML?
             loc.firstChild:
             loc.firstChild.nextSibling;
    
             var th = document.getElementById(loc);
             if(th != null){
               //th.innerHTML=th.innerHTML=='+'?'-':'+';
                foc.innerHTML=foc.innerHTML=='+'?'-':'+';
             }
    
             foc=loc.parentNode.nextSibling.style?
             loc.parentNode.nextSibling:
             loc.parentNode.nextSibling.nextSibling;
          foc.style.display=foc.style.display=='block'?'none':'block';}}
    
    if(!document.getElementById)
       document.write('<style type="text/css"><!--\n'+
          '.more_content{display:block;}\n'+
          '//--></style>');
    //-->
     
    Code (markup):
    HTML,
    <span class="more_intro"><a href="javascript:void(0)" onClick="dsp(this)">Click here for further info...</a></span>
          
          <span class="more_content">Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, <br />
         </span>
    Code (markup):
    Thanks :)
     
    mark_s, Jan 11, 2008 IP
  2. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Anyone? :)
     
    mark_s, Jan 14, 2008 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Split the content with a space (' ') than count the length. Simple.
     
    MMJ, Jan 14, 2008 IP
  4. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Simple to someone who knows Javascript :)

    I wouldn't know what to do from your post MMJ but thanks for your input.
     
    mark_s, Jan 14, 2008 IP
  5. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Who wrote your current code?
     
    MMJ, Jan 14, 2008 IP
  6. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    mark_s, Jan 14, 2008 IP
  7. rkquest

    rkquest Well-Known Member

    Messages:
    828
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    140
    #7
    if you're going to use MMJ's suggestion.. it's implementation would be similar to this..

    (the body part of your page)
    
    <span class="more_intro"><a id="theLink" href="javascript:void(0)" onClick="dsp(this)">Click here for further info...</a></span>
          
          <span id="more_content" class="more_content">Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, Further info, <br />
         </span>
    
    <script type="text/javascript">
    var wordCount = document.getElementById('more_content').innerHTML.split(" ").length;
    document.getElementById('theLink').innerHTML += " ("+wordCount+" words)";
    </script>
    
    Code (markup):
    take note of the element IDs.

    It doesn't give the exact number of words but there should be a work around.. :p
     
    rkquest, Jan 14, 2008 IP
  8. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The way the script works now means I don't need to specify a unique ID for each expandable bit of text, that's very important to me.. with what you give I think I would need to do that otherwise all the links would have the same word count, right?
     
    mark_s, Jan 14, 2008 IP
  9. rkquest

    rkquest Well-Known Member

    Messages:
    828
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    140
    #9
    Yeah.. without the ID's that won't work. But then without using ID's, it will be very difficult to match the expandable blocks with the link.

    I'm just curious, why can't you use a unique ID for each expandable block?
     
    rkquest, Jan 14, 2008 IP
  10. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I have this script implemented into my forum using custom BBC codes I have coded in. Having it use an ID would be to difficult for me to implement but without IDs is quite simple.
     
    mark_s, Jan 15, 2008 IP
  11. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I use my forum as a CMS and I use this script by the custom BBC codes I have made - for example, [more_intro]Read on[/more_intro][more]Continued information[/more]. Having it use an ID would make it very difficult for me to implement into my forum.
     
    mark_s, Jan 15, 2008 IP
  12. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Is it actually possible to do it without needing a unique ID every time?
     
    mark_s, Jan 15, 2008 IP