PDA

View Full Version : Truncate Product Description


hepmarketing
14-Apr-2008, 06:32 PM
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.


<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.

Duncan Rounding
14-Apr-2008, 07:10 PM
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.

NormanRouxel
14-Apr-2008, 09:37 PM
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.

NormanRouxel
14-Apr-2008, 09:38 PM
Also with embedded HTML, you're going to run into problems if the 40th character is within an HTML tag.

NormanRouxel
14-Apr-2008, 10:03 PM
This might work.

Put this in the HEAD of your overall layout.

<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

<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.