1 Replies - 1077 Views - Last Post: 25 June 2013 - 03:17 PM

#1 pezzer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-May 13

Javascript for URL word count

Posted 04 May 2013 - 06:47 AM

Hi Everyone,

I'm very curious, how can you code a function which given a URL would provide an array containing a count of all words on the URL given? I've seen something similar here:

http://www.webmaster...ysis-tool.shtml

Cheers
Is This A Good Question/Topic? 0
  • +

Replies To: Javascript for URL word count

#2 Brunus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-June 13

Re: Javascript for URL word count

Posted 25 June 2013 - 03:17 PM

Retrieve the contents from the page as sited here:
http://www.webdevelo...e-text-from-URL

The example is (ajax):

function doAjax(url , getOrPost , callBackFn , params ,contentIsXml ){
    var xmlHttp = null;
    if(typeof XMLHttpRequest!='undefined') {
    xmlHttp = new XMLHttpRequest();
        //firefox based
     }else{
       //ie based
     try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
    try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        try {
        xmlHttp = new XMLHttpRequest();
        } catch (e) {
        alert('sorry ,this browser doesnt support xmlhttp the way youve done it,do some more research'); return ;
        }
    }
     }
    }
   if(! ( (contentIsXml!=undefined)?contentIsXml:true){
     xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
    if(url!=undefined){url+='?now='+(new Date().getTime() ); }else{return;}
    xmlHttp.open(getOrPost, url, true);
    xmlHttp.onreadystatechange = function() {
           if (xmlHttp.readyState==4 && xmlHttp.status==200) {
                  //all done !
                 callBackFn(xmlHttp);
           }else{
                  //display a 'loading...' message if you want...
           }
    } 
    if(params!=undefined){
       xmlHttp.send(params);
    }else{
      xmlHttp.send(null);
   }
}  
}




The php

doAjax('path/to/txtfile.txt','GET', readDone);

function readDone(req){
  alert( 'the text says  :\n '+req.responseText);
}  



Then once you have the data you would parse through it like this:

var str="How are you doing today?";
var n=str.split(" "); 



reference: http://www.w3schools...jsref_split.asp

You would use conditionals in order to keep track of which words have been parsed out
and then add one to count for that word.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1