Solved

Show a specified number of words with JavaScript?

Posted on 2004-04-24
12
155 Views
Last Modified: 2012-06-27
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
Comment
Question by:mhermansson
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 10906973
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
 

Author Comment

by:mhermansson
ID: 10906989
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
 
LVL 6

Expert Comment

by:Lord_McFly
ID: 10907294
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
 
LVL 8

Expert Comment

by:plq
ID: 10911677
What about the split function

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

for (var i = 0; i < wordarray.length; i++)
{
   alert (wordarray[i]);
}
0
 
LVL 8

Expert Comment

by:plq
ID: 10911684
Then you can join them back for redisplaying just the first 20

var mystring2 = wordarray.join(" ");

document.body.div1.innerText = mystring2;
0
 

Author Comment

by:mhermansson
ID: 10911741
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Expert Comment

by:Lord_McFly
ID: 10911775
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
 

Author Comment

by:mhermansson
ID: 10911795
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
 
LVL 6

Accepted Solution

by:
Lord_McFly earned 500 total points
ID: 10911945
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
 
LVL 6

Expert Comment

by:Lord_McFly
ID: 10911959
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
 

Author Comment

by:mhermansson
ID: 10937303
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
 

Author Comment

by:mhermansson
ID: 10937355
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now