Announcement

Collapse
No announcement yet.

Truncate Product Description

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Truncate Product Description

    I want to truncate the product description and only display the first 40 characters or so. i have ready many threads and have come up with the code below. This only works with plain text in the description. Once I put in the HTML tags for example: !!< Product description goes here. >!!. This script will not write the description.


    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function writeJS(){
    var str='NETQUOTEVAR:PRODUCTDESCRIPTION';
    document.write(str.substring(0,40) + '...');
    }
    writeJS();
    //-->
    </script>
    Any suggestions on what I am forgettting or doing wrong. i am using developer 7.

    #2
    If you search a little deeper (in the v8 forum) you should find some php that does a similar thing and resolves that problem (by Norman).

    If you find that then you may be able to change it to js and use it it in v7.

    Comment


      #3
      What you've written will break if there are any newlines in your Product Description. Newlines are usually replaced with <br> but not within embedded HTML.

      You might have to put your embedded HTML all on the same line as the surrounding text.
      Norman - www.drillpine.biz
      Edinburgh, U K / Bitez, Turkey

      Comment


        #4
        Also with embedded HTML, you're going to run into problems if the 40th character is within an HTML tag.
        Norman - www.drillpine.biz
        Edinburgh, U K / Bitez, Turkey

        Comment


          #5
          This might work.

          Put this in the HEAD of your overall layout.
          Code:
          <script type="text/javascript">
          <!--
          function truncate(anchor){
            var desc = document.getElementById('pd-' + anchor).innerHTML;
            // invent some code here to deal with HTML tags near the 40th character
            document.write(desc.substring(0,40) + '...');
          }
          // -->
          </script>
          Replace NETQUOTEVAR:PRODUCTDESCRIPTION with
          Code:
          <span style="display:none; visibility:hidden;" id="pd-NETQUOTEVAR:PRODUCTANCHOR">NETQUOTEVAR:PRODUCTDESCRIPTION</span>
          <script type="text/javascript">
          <!--
          truncate('NETQUOTEVAR:PRODUCTANCHOR');
          // -->
          </script>
          I'm not sure if search engines will be happy with the hidden text.
          Norman - www.drillpine.biz
          Edinburgh, U K / Bitez, Turkey

          Comment

          Working...
          X