Generating Excerpt text with sitelink

I have a varchar field full of text and I want to be able to just show a 100 character piece of text and show a "More ..." link at the end of the piece. When the user clicks "More ...", I would like the page to expand and display the rest of the text.

I'm guessing the show / hide function could be done with jQuery, but I wasn't sure if ASP has some function to effectively split a varchar text field in two?

My content is currently being pulled onto the page using;

<%=StripHTML(rspropertyresults.Fields.Item("ContentDetails").Value)%>

      

Uses this function to highlight any HTML tags;

<%
Function stripHTML(strHTML)
  ''Strips the HTML tags from strHTML

  Dim objRegExp, strOutput
  Set objRegExp = New Regexp

  objRegExp.IgnoreCase = True
  objRegExp.Global = True
  objRegExp.Pattern = "<(.|\n)+?>"

  ''Replace all HTML tag matches with the empty string
  strOutput = objRegExp.Replace(strHTML, "")

  ''Replace all < and > with &lt; and &gt;
  strOutput = Replace(strOutput, "<", "&lt;")
  strOutput = Replace(strOutput, ">", "&gt;")

  stripHTML = strOutput    ''Return the value of strOutput

  Set objRegExp = Nothing
End Function
%>

      

+2


source to share


3 answers


Try to use this as a starting point.

<p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas massa lectus, pulvinar vel scelerisque eget, 
    rutrum et nisi. Mauris semper viverra lorem sit amet faucibus. Fusce egestas metus sit amet lectus interdum 
    sollicitudin. Maecenas accumsan metus scelerisque tortor lobortis et pretium nibh cursus.
</p>
<script type="text/javascript">
    $(function() {
        var textToHide = $('p').text().substring(100);
        var visibleText = $('p').text().substring(1, 100);

        $('p')
            .html(visibleText + ('<span>' + textToHide + '</span>'))
            .append('<a id="read-more" title="Read More" style="display: block; cursor: pointer;">Read More&hellip;</a>')
            .click(function() {
                $(this).find('span').toggle();
                $(this).find('a:last').hide();
            });

        $('p span').hide();
    });
</script>

      

So, I made two variables here: one to hold the first 100 characters ("visibleText") and one to store the rest ("textToHide").

Then we tell jQuery to find each paragraph of the paragraph (you will most likely want to define a more specific selector), wrap the text with a span tag and put everything back on the visible text, add a link at the end of it all to show the text that we will be hiding , and finally, assign a click event for that.



The click function just looks for all the spans in the paragraph, switches them to view (the function show()

might be the best choice here, actually), and then hides the Read More link.

Finally, we make sure our paragraph range tags start hiding. You can actually create a CSS ( p span {display: none;}

) rule so that the text is still hidden, but runs faster than JavaScript. The jQuery function show()

will still override this css rule when called.

This should do it.

+12


source


Expanding on Phil Wheeler's example, here's an example that shows applying this to every td element in a table using the jQuery Each function .

<html>
<head>
    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js" 
            type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("td").each(
                function( intIndex ) {
                    var textToHide = $(this).text().substring(100);
                    var visibleText = $(this).text().substring(1, 100);

                    $(this)
                        .html(visibleText + ('<span>' + textToHide + '</span>'))
                        .append('<a id="read-more" title="Read More" style="display: block; cursor: pointer;">Read More&hellip;</a>')
                        .click(function() {
                            $(this).find('span').toggle();
                            $(this).find('a:last').toggle();
                        });

                    $(this).find("span").hide();
                }
            )

        });
    </script>
</head>
<body>
    <table border="1">
        <tr>
            <td>
                "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
            </td>
            <td>
                "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
            </td>
        </tr>
    </table>
</body>

      



+1


source


All you need is doable and pretty easy when you hug it. I'm not a jquery person (Mootools for me!) So can't help you.

For the ASP truncation feature though, you need to be careful. You have the right idea, although you are removing the html, otherwise you can leave the open closed tag open (the unclosed div or script tag can really mess up your layout).

I would:

  • Divide it first.
  • If the length was less than what you wanted to truncate it, just display it
  • Another sensible use of Left (your_string, 300) is to grab only the first 300 characters (for example), then add "..." to the end or whatever. Easy and enjoyable.

You can try to get fancy and try to split it into the next space so you don't split the words in half, but you can't remember how to do it in VBScript. JS is not a problem!

0


source







All Articles