The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
Need a little code
I'm using this code to display forum thread titles on a custom vb page:
<script type="text/javascript" src="external.php?forumids=111,112,113,114,115,133 ,148,154&type=js"></script> <script language="" type="text/javascript"> <!-- for (x = 0; x < 3; x++) { document.writeln("<a href=\"showthread.php?t="+threads[x].threadid+"\">"); document.writeln("<b>"+threads[x].title+"</b><br />"); document.writeln("</a>"); document.writeln("Posted "+threads[x].threaddate+" at "+threads[x].threadtime+" by "+threads[x].poster+"<br />"); } //--> </script> I'm trying to figure out how to limit the number of characters that are displayed in the title. document.writeln("<b>"+threads[x].title+"</b><br />"); I tried inserting this code inside the javascript but I can't seem to get it to work <?php $len = 30; if (strlen($thread['title']) > $len) { $thread['title'] = substr($thread['title'],0,$len); $thread['title'] .= "..."; } else { $thread['title'] = $thread['title']; } ?> Ideally I would like to limit the character length of threads.title and append a ... when it gets cut off. Any coders out there have a solution for me? Thanks |
#2
|
|||
|
|||
The problem is you are trying to use PHP within javascript; luckily, javascript provides the same string manipulation functions that you need. The following should work (I don't know if you explicitly need to declare the string as an object or not, let me know if it isn't working):
Code:
var max_title_length = 30; for(...) { if(threads[x].title.length > max_title_length) { threads[x].title = threads[x].title.substr(0, max_title_length) + '...'; } // ... format/output code } |
#3
|
|||
|
|||
thanks Danny,
I entered your code exactly as you posted but it didn't work. I was able to get the following code to work: <script language="" type="text/javascript"> <!-- for (x = 0; x < 3; x++) { document.writeln("<a href=\"showthread.php?t="+threads[x].threadid+"\">"); var tread = threads[x].title; if (tread.length > 30) { document.writeln(tread.substring(0,30)) document.writeln("... ") } else { document.writeln(tread) document.writeln(" ") } document.writeln("</a>"); document.writeln("Posted "+threads[x].threaddate+" at "+threads[x].threadtime+" by "+threads[x].poster+"<br />"); } //--> </script> I'm sure it could be cleaned up. One thing I still need is code that will count out to the character limit and then break at the end of the closest word such that the last word in the string is not a partial word. Any idea how to add that? Thanks |
Thread Tools | |
Display Modes | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|