Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: ajax question

  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ajax question

    Ok i am working on an app for the palm pre. and the palm pre can use ajax, css, html, javascript, and php

    I have a very rough version of the app i made, but it only displays the whole webpage within the app. But what i was wondering if there was an ajax script (or something else) that grabs the desired text from the selected webpage and can display the desired text only.
    So say I wanted to know what the meaning of the name Alex meant
    I just want it to display;

    ALEX
    Gender_____"Male"
    Origin______"Greek"
    Meaning____"Protector of men"

    Without all the ads they have on the site
    http://www.zelo.com/firstnames/findr...e=Alex&x=0&y=0


    Is this possible.

  • #2
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    I would say yes. But grabbing text from other websites is not a very reliable (if I understood well, that zelo.com website is not the website where your ajax code will be running, right?).

    I used that method in one of my websites to grab some info, say a part of a webpage on another server) but there are a few issues with that:

    1. On Zelo.com, you need to find some HTML code that is ALWAYS the same and ALWAYS at the same place on EACH page from which you need to grab information. You have to analyze a few pages and see if you can find some code that is unique to the portion of the page from which you need to take the information AND identical on every page.

    2. Your code will always be tributary to zelo.com. If they change anything in their design, you will probably have to change your code as well.

    3. I have no idea if this could be reprehensible in terms of copyright, etc.

    Below is the code I used in my website, I just changed the URL with zelo.com:

    PHP Code:
    function show($new) {
    //BELOW IS THE URL OF THE PAGE ON ZELO THAT INTERREST YOU. OF COURSE, YOU MAY PROBABLY WANT TO COMPUTE THE PAGE NAME AND VALUE 'ALEX' TO MATCH YOUR CODE            
            
    $URL "http://www.zelo.com/firstnames/findresults.asp?name=Alex&x=0&y=0";
            
    $COUNTER 0;
            
    $STARTSHOW false;
            
    $fd fopen ($URL"r");
            
            while (!
    feof($fd)) {
                
                
    $buffer fgets($fd4096);
            
    //REPLACE THE XXXXXX BELOW WITH THE PORTION OF CODE THAT YOU IDENTIFIED AS COMMON TO ALL PAGES YOU NEED INFO FROM, AS BEING THE BEGINING PART OF THE INFORMATION YOU NEED
                
    if (strpos($buffer"XXXXXXX") == true) {
        
                    
    $STARTSHOW true;
                }    
    //YOU MAY WANT TO ADJUST THE COUNTER BELOW TO OVER 5000 IF THE CODE IS LONGER THAN THAT BUT I THINK IT SHOULD BE FINE IN YOUR CASE    
                
    if (($STARTSHOW == true) and ($COUNTER 5000)) {
        
    //BELOW IS SOMETHING YOU MAY NEED TO DO IF YOU NEED TO REPLACE SOME HTML BEFORE DISPLAYING IT ON YOUR WEBSITE
                    
    $buffer str_replace("<area ","<area target=\"_blank\" "$buffer);
                    
                    
                    echo 
    $buffer;
    //REPLACE THE XXXXXX BELOW BY THE BIT OF HTML CODE THAT YOU IDENTIFIED TO BE IDENTICAL ON EACH PAGE YOU NEED TO GRAB INFO, AS BEING THE END OF THE PART YOU ARE INTERRESTED IN                
                    
    if (strpos($buffer"XXXXXXX") == true) {
                        
    $COUNTER 9999;
                    }    

                }
                            
                
    $COUNTER++;
            }
            
    fclose ($fd);
        } 
    Hope this helps!
    Chuck Norris counted to infinity.
    Twice.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    load the document into a DOM object or element, and use the dom to extract the parts you need.

    it's a LOT simpler and more robust than chopping strings that hings on layout and presentation...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    That sounds nice indeed. I'd love to see an example of what you are talking about... Could you post something?
    Chuck Norris counted to infinity.
    Twice.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    here's an example i threw together for you.

    tested w/ff3.5 on the linked page.

    displays text about as requested, you can clean it up from here, i'm going to bed...


    Code:
    //stock functions:
    function text(elm){return elm.data || elm.value || elm.innerText || elm.textContent || elm.innerHTML || "";}
    function IO(U, V) {
        var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
        X.open(V ? 'PUT' : 'GET', U, false);
        X.setRequestHeader('Content-Type', 'text/html')
        X.send(V ? V : '');
    return X.responseText;}
    
    
    //customize this code:
    var url=window.location;
    var txt=IO(url); //you might need to provide txt's value some other way...
    
    
    
    //custom rakes:
    var elm=document.createElement("span");
    elm.innerHTML = txt.replace(/^[\w\W]*<body [\w\W]+?>([\w\W]+)<\/body>[\w\W]*$/mg,"$1");
    var tabs=elm.getElementsByTagName("table");
    var buffer="";
    for(var i=0,mx=tabs.length;i<mx;i++){
      var it=tabs[i];
      if(it.width==="70%"){ 
        buffer+= text(it.rows[1]);
        buffer+= text(it.rows[2])
        buffer+= text(it.rows[3])
      }//right table?
    }//next
    
    
    alert(buffer);
    if it flops on the pre, try pasting it into firebug's console when viewing the page to get a feel of how it works.

    perhaps there are related Pre dom methods you can utilize to imitate the general pattern if it won't work.

    also, not sure about the rules on the Pre, but this code can only be run from the site linked when using a real browser, same origin policy...

    If the code worked from anywhere on a real browser, that would be considered hacking and i couldn't answer your question publicity.

    if you can get a string of the page's text, you can set the var txt to that, and throw away the IO function.

    good luck!
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •