Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

Show a specified number of words with JavaScript?

Hello,

I'm trying to create a JavaScript that grabs let's say the 20 first words from a text and shows them on a specific page, like some sort of preview.

Since the text it would grab itself is in another javascript, there should be something that tells this script to start grabbing from a specific mark, say like rownumber or preferrably after a certain word och word sequence, example "newsArray[T].body = ".

And needless to say, I haven't got a clue how to do it or if it's even possible.

Anyone with a quick and easy solution? No need to be a fancy one.

Cheers
/M
0
mhermansson
Asked:
mhermansson
  • 5
  • 4
  • 2
  • +1
1 Solution
 
ZvonkoSystems architectCommented:
So you have a preView() function and a fullTex() function.
The preview function needs two paramteres, the text or reference then text, and the ammount of words to be displayed.
Is this what you want?
0
 
mhermanssonAuthor Commented:
It sounds kinda right, as long as you could specify exactly where from it should begin to preview the text...? I.e. filename, rownumber, words etc etc.

Sorry if I'm not making any sense, this is not really my cup of tea and I'm confused. Just a heads up if I begin to ramble :)
0
 
Lord_McFlyCommented:
Ok, I have a small script that will get the first x number of words from a block of text. What I need to know is where is the orginal text sourced from.

Copy and paste, have a look and then explain a little more what you need - maybe this will act as a stating point...

<html>
<head>
<title>Example</title>
<script language="JavaScript">
<!--
var example
var title
var article = new Array(1)


article[0] = "News Article 1";
article[1] = article[1] + "The group said it had earned $111m (£62.7m), compared with a $10m "
article[1] = article[1] + "(£5.8m) loss for the same time last year. Excluding certain items, "
article[1] = article[1] + "the American online trading giant posted a profit of $97m (£57.6m) "
article[1] = article[1] + "up 141% from last year, beating analyst forecasts. The group has "
article[1] = article[1] + "benefited from aggressive pricing and free shipping incentives "
article[1] = article[1] + "which helped lift sales by 41%."

function GetArticle(Part,Words)
      {
            if(Part==0)
                  {
                        document.writeln(article[0])
                  }
            else
                  {
                        var words = article[1].split(" ")
                        var i
                        var newbody = ""
                        
                        for(i=0; i<=Words; i++)
                              {
                                    newbody = newbody + words[i] + " "
                              }
                        
                        
                        newbody = newbody.substr(0,newbody.length -1) + "...";
                        document.writeln(newbody)
                  }
      }
//-->
</script>
</head>
<body>
      <table width="500" cellspacing="0" cellpadding="2" border="0">
            <tr>
                  <td><script language="javascript">GetArticle(0,0)</script></td>
            </tr>
            <tr>
                  <td><script language="javascript">GetArticle(1,20)</script></td>
            </tr>
      </table>
</body>
</html>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
plqCommented:
What about the split function

var wordarray = mystring.split(" ", 20);

for (var i = 0; i < wordarray.length; i++)
{
   alert (wordarray[i]);
}
0
 
plqCommented:
Then you can join them back for redisplaying just the first 20

var mystring2 = wordarray.join(" ");

document.body.div1.innerText = mystring2;
0
 
mhermanssonAuthor Commented:
Lord McFLy, I probably didn't get your script 100% right, but as far as I could see you grab the words from the same file, right?

I have a separate script that I want to grab them from, is there anyway to tell your script to look in another file?

But I guess I could just put that function in my existing script, right? I'll check that up and get back to you.

Thanks!
0
 
Lord_McFlyCommented:
If you can include the code (or your page) so that I can see how you get say the Title or the Article content then I'll hopefully be able to mix up the 2 - at the mo I don't know where that info comes from :)
0
 
mhermanssonAuthor Commented:
Ok, I tested the script standalone, and this is exactly what I am looking for (though I got a "undefined" error).

But again, I got confused when I tried to implement it in my existing script. Not sure it will work at all. I have to two seperate scripts for listing news and stuff: one contains all the functions and the array. The other one contains all the text.

This is the setup:
===========
var guestArray = new Array();
function guest() {
this.title = '';
this.author = '';
this.authorEmail = '';
this.date = '';
}
function listguest(n) {
if (n == 0 || n > guestArray.length)
n = guestArray.length;
for (var i = 0; i < n; i++) {
document.write('<div class="news">\n<h3>' + guestArray[i].title + '</h3>\n');
document.write(guestArray[i].body + '\n');
var cat;
if (guestArray[i].category == 1)
cat = '<div class="category2">Dagbok';
document.write(cat + '</div>\n<p class="info">Skrivet av <a href="mailto:' + guestArray[i].authorEmail + '">' + guestArray[i].author);
document.write('</a> | ' + guestArray[i].date + '</p>\n</div>');
}

and this is the other one:
===========
var T = 0;
guestArray[T] = new guest();
guestArray[T].title = '';
guestArray[T].category = 1; // 1 = Dagbok
guestArray[T].author = '';
guestArray[T].authorEmail = '';
guestArray[T].date = '2004-mm-dd';
guestArray[T].body = '<p></p>';
T++;

Does this make any sense? Can I implement your script somehow in this...?
0
 
Lord_McFlyCommented:
Ok, I couldn't test it completely but the logic seems right to me.

I have included an update of my function...

function GetArticle(Body,NoWords)
      {
            var words = Body.split(" ")
            var i
            var newbody = ""
                        
            if(words.length-1 < NoWords)
                  {
                        NoWords = words.length-1
                  }
                        
            for(i=0; i<=NoWords; i++)
                  {
                        newbody = newbody + words[i] + " "
                  }
                        
                        
            newbody = newbody.substr(0,newbody.length -1) + "...";
            
            document.write(newbody + '\n');
      }


You need to change a line in your funtion as follows...

function listguest(n)
      {
            if (n == 0 || n > guestArray.length)
                  n = guestArray.length;
                  
            for (var i = 0; i < n; i++)
                  {
                        document.write('<div class="news">\n<h3>' + guestArray[i].title + '</h3>\n');
                        //REM this line...
                        //document.write(guestArray[i].body + '\n');
                        //Try this...
                        GetArticle(guestArray[i].body,20)
                        
                        var cat;
                        
                        if (guestArray[i].category == 1)
                              cat = '<div class="category2">Dagbok';
                              
                        document.write(cat + '</div>\n<p class="info">Skrivet av <a href="mailto:' + guestArray[i].authorEmail + '">' + guestArray[i].author);
                        document.write('</a> | ' + guestArray[i].date + '</p>\n</div>');
                  }
      }
0
 
Lord_McFlyCommented:
I must admit still that I can't see where you actually get the article body from - I can see this bit which is the array that holds the information but how do you populate it?

var T = 0;
guestArray[T] = new guest();
guestArray[T].title = '';
guestArray[T].category = 1; // 1 = Dagbok
guestArray[T].author = '';
guestArray[T].authorEmail = '';
guestArray[T].date = '2004-mm-dd';
guestArray[T].body = '<p></p>';
T++;
0
 
mhermanssonAuthor Commented:
Sorry about the delay, but I tried it and hey; it kinda works!

First, the article body is here: guestArray[T].body = '<p>bla bla bla</p>';.

When I changed that line you suggested, I only got the guestArray[T].title = ''; stuff, not the body.
Clear improvement though, but do you have any idea whats wrong? I'll tinker with it a bit to see if I can understand anything at all.

Cheers!
0
 
mhermanssonAuthor Commented:
Hey, my bad! It works!

Didn't pay enough attention to your new function, missed a bit there.

Excellent work, thanks a bunch!!

Cheers
/marcus
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now