The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Help me please??
Discuss Help me please?? in the JavaScript Development forum on Dev Shed. Help me please?? JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 6th, 2013, 05:30 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 10
Time spent in forums: 52 m 13 sec
Reputation Power: 0
|
|
|
Help me please??
can anyone help me how can I extract text from webpage and count occurence of the words and the result should display in table and user should select if the order is alphapatic or frequencey  please help me ??
thanks in advance
|

February 7th, 2013, 12:47 AM
|
 |
Contributing User
|
|
|
|
This will let you define the word your looking for and display results accordingly:
Code:
<!DOCTYPE html>
<html>
<head>
<script>
var count = 0;
var cached="no";
function counter(word,counTitle,wordTitle)
{
content = document.getElementsByTagName("body")[0].innerHTML.split(" ");
for (i=0;i<content.length;i++) {
content[i] = content[i].replace("\n"," ");
if (content[i].match(word)) {
count++;
}
}
var countHeadingRow = document.createElement("tr");
var HeadingTD = document.createElement("td");
var HeadingTD2 = document.createElement("td");
var newTR = document.createElement("tr");
var newTD = document.createElement("td");
var newTD2 = document.createElement("td");
HeadingTD.innerHTML = counTitle;
HeadingTD2.innerHTML = wordTitle;
newTD.innerHTML = count;
newTD2.innerHTML = word;
// create heading
if (cached != "yes") {
document.getElementById("wordData").appendChild(countHeadingRow);
document.getElementById("wordData").getElementsByTagName("tr")[0].appendChild(HeadingTD);
document.getElementById("wordData").getElementsByTagName("tr")[0].appendChild(HeadingTD2);
cached="yes";
}
// create new data rows for each word/quanity
document.getElementById("wordData").appendChild(newTR);
var maxRows = document.getElementById("wordData").getElementsByTagName("tr").length - 1;
document.getElementById("wordData").getElementsByTagName("tr")[maxRows].appendChild(newTD);
document.getElementById("wordData").getElementsByTagName("tr")[maxRows].appendChild(newTD2);
// reset all
count = 0;
content = [];
}
document.onreadystatechange = function() {
if (document.readyState=="complete") {
// add "counter" function and define parameters, too find case sensitive words & word quanities
counter("rock","Amount Displayed","Word Displayed");
counter("wanna","Amount Displayed","Word Displayed");
counter("Turn","Amount Displayed","Word Displayed");
counter("go","Amount Displayed","Word Displayed");
counter("Go","Amount Displayed","Word Displayed");
counter("No","Amount Displayed","Word Displayed");
}
}
</script>
</head>
<body>
<!-- demo content - begin -->
<pre style="text-align:center">
I wanna rock! rock!
I wanna rock! rock!
I want to rock rock!
I wanna rock! rock!
Turn it down you say,
Well all I got to say to you is time again I say, "No!"
No! No, No, No, No, No!
Tell me not to play
Well, all I got to say to you when you tell me not to play,
I say, "No!"
No! No, No, No, No, No!
So, if you ask me why I like the way I play it
There's only one thing I can say to you
I wanna rock! rock!
I wanna rock! rock!
I want to rock rock!
I wanna rock! rock!
There's a feelin' that
I get from nothin' else and there ain't nothin' in the world
That makes me go!
Go! Go, go, go, go, go!
Turn the power up
I've waited for so long so I could hear my favorite song so,
Let's go!
Go! Go, go, go, go, go!
When it's like this I feel the music shootin' through me
There's nothin' else that I would rather do
I wanna rock! rock!
I wanna rock! rock!
I want to rock rock!
I wanna rock! rock!
</pre>
<!-- demo content - end -->
<br/>
<br/>
<br/>
<table id="wordData" border="1" width="100%"></table>
</body>
</html>
|

February 7th, 2013, 08:10 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 10
Time spent in forums: 52 m 13 sec
Reputation Power: 0
|
|
|
Thanky fo helping but the count should be from another web page so it let user to type the url of the page to count and the result should be in a table .... How can I solve this
|

February 7th, 2013, 07:32 PM
|
 |
Contributing User
|
|
|
|
You could go about doing this a couple different ways, but the main idea is..., that your going to have to scrape the page's content and get text/responseText back into the "content" variable. You can set-up a server side page to get the content's of a page once it has been queried. A basic example in my chosen server side language would be:
PHP Code:
scrapper.php
<?php
header("Content-Type:application/javascript");
$defineURL = $_GET["url"];
$contents = file_get_contents("".$defineURL."");
if (isset($defineURL)) {
echo "var content=\"$content\";\n\n";
}
else {
echo "var content=\"No Content Found!\";\n\n";
}
?>
Then add this to your page (remove the content variable declaration; that I originally had in the code.... ie. "var content;"); with an external javascript link... ie. "<script type="text/javascript" src="scrapper.php"></script>" or you could get the responseText with AJAX and insert it into the "content" variable.
Or... an alternative would be to use and incorporate the YQL Module for YUI; which is a screen scraper plugin/API from Yahoo's YUI 3.0 Framework.
Or... you could screen scrape the page and serve it as plain text or html and include it in an iframe (within the page the search is occurring from) and then have the "content" variable store the innerHTML of the contentWindow || contentDocument of the iframe.
Or... you could just do away with the client side aspect (javascript) and do this completely server side. In my opinion, this option... would be the best way to go.
So... yeah, you have several ways you could do this. Do some online searching for "screen scraping" and/or "server side screen scraping" and/or "client side screen scraping" and/or "ajax screen scraping" and/or "javascript screen scraping". Those are all good places to start from... good luck. 
|

February 8th, 2013, 06:57 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 10
Time spent in forums: 52 m 13 sec
Reputation Power: 0
|
|
|
I have put the code to get the url from the user to count
however i do not know what should I have to put in the counter()??? to diblay the result
here is my code
[code]
<!DOCTYPE html>
<html>
<head>
<script>
var count = 0;
var cached="no";
function counter(word,counTitle,wordTitle)
{
content = document.getElementsByTagName("body")[0].innerHTML.split(" ");
for (i=0;i<content.length;i++) {
content[i] = content[i].replace("\n"," ");
if (content[i].match(word)) {
count++;
}
}
var countHeadingRow = document.createElement("tr");
var HeadingTD = document.createElement("td");
var HeadingTD2 = document.createElement("td");
var newTR = document.createElement("tr");
var newTD = document.createElement("td");
var newTD2 = document.createElement("td");
HeadingTD.innerHTML = counTitle;
HeadingTD2.innerHTML = wordTitle;
newTD.innerHTML = count;
newTD2.innerHTML = word;
// create heading
if (cached != "yes") {
document.getElementById("wordData").appendChild(countHeadingRow);
document.getElementById("wordData").getElementsByTagName("tr")[0].appendChild(HeadingTD);
document.getElementById("wordData").getElementsByTagName("tr")[0].appendChild(HeadingTD2);
cached="yes";
}
// create new data rows for each word/quanity
document.getElementById("wordData").appendChild(newTR);
var maxRows = document.getElementById("wordData").getElementsByTagName("tr").length - 1;
document.getElementById("wordData").getElementsByTagName("tr")[maxRows].appendChild(newTD);
document.getElementById("wordData").getElementsByTagName("tr")[maxRows].appendChild(newTD2);
// reset all
count = 0;
content = [];
}
document.onreadystatechange = function() {
if (document.readyState=="complete") {
// add "counter" function and define parameters, too find case sensitive words & word quanities
}
}
</script>
</head>
<body>
<p>Web Page URL : <input id = "url" name="url" size=25 type="Text"/></p>
<a href="javascript:coutner(document.getElementById('url').value)"> count word in document document</a></br>
<table id="wordData" border="1" width="20"></table>
</body>
</html>[code]
|

February 8th, 2013, 07:12 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
please read the replies you get. web_loone08 has made a long list of possible solutions, which you just ignored.
You cannot access other domains with JavaScript. It's not allowed. If it was, that would be a horrible security hole, because other websites could use JavaScript to telecontrol your browser and, for example, make PayPal transactions on your behalf. So what you're trying there is impossible.
If you want to count words on another domain, you need a server script (written in PHP or whatever). But web_loone08 already said this, so simply read his post.
|

February 8th, 2013, 07:45 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 10
Time spent in forums: 52 m 13 sec
Reputation Power: 0
|
|
|
because what I mean is get text from other html file and count the occurence of the word using java script ??
|

February 8th, 2013, 07:57 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
I understand what you're trying to do, but you cannot do this with JavaScript. You just can't. It's impossible. It's not allowed for security reasons.
You need to count the words on your server with PHP or Ruby or Python or Perl or whatever server-side scripting language you are familiar with.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|