Help need with this script on how to fetch text from a site using JS?

Discussion in 'JavaScript' started by sandeepdude, Oct 6, 2009.

  1. #1
    Hello

    My script fetches the image from a given url and displays it in another page.

    newElement = document.createElement('div');
    	newElement.innerHTML = '<img src="http://www.adomain.com/myimage.gif" />';
    	thisElement.insertBefore(newElement, thisElement.childNode);
    
    PHP:
    Now instead of the image,i want to fetch the text in that page using its ID

    How can i modify this script to do it..

    Please help.

    Regards,
    Sandeep
     
    sandeepdude, Oct 6, 2009 IP
  2. theapparatus

    theapparatus Peon

    Messages:
    2,925
    Likes Received:
    119
    Best Answers:
    0
    Trophy Points:
    0
    #2
    theapparatus, Oct 6, 2009 IP
  3. sandeepdude

    sandeepdude Well-Known Member

    Messages:
    1,741
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #3
    sandeepdude, Oct 6, 2009 IP
  4. jpinheiro

    jpinheiro Peon

    Messages:
    1,211
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    0
    #4
    you would need to use php regex I can attempt to code it just givce me a page that you want to get the text from.

    -john
     
    jpinheiro, Oct 6, 2009 IP
  5. lawracri

    lawracri Peon

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    JS can't do this, must use JScript (ActiveX)
     
    lawracri, Oct 6, 2009 IP
  6. Jaguarjace

    Jaguarjace Guest

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i don't know if this would help but you could set a iframe with the id in the url like this:
    
    <iframe src="http://www.abc.net.au/news/default.htm#headlines" width="500px" height="50px" frameborder="0" scrolling="no"></iframe>
    
    Code (markup):
    the main topics section has an id called headlines, so i used an iframe to only show this section so i can put it on my website.
    All you have to do is change the iframe's width, height and the source URL.
    Original URL: http://www.abc.net.au/news/default.htm
     
    Last edited: Oct 6, 2009
    Jaguarjace, Oct 6, 2009 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    that's not strictly true. javascript does have restrictions on accessing anything cross-domain through request/ajax/XHR, yes. but you can use JSONP to fetch JSON data from other domains in pure javascript. you can also use COMET to embed javascript through push on remote hosts.

    to do what you ask, its a simple ajax setup - you need to code a local php file that fetches a url through curl (or fopen if allowed) and outputs it. your javascript can then reference your local file instead as a proxy.

    for example:
    
    // javascript fetching the js and setting it into something called "output"
    
    // this is in mootools but use whatever ajax framework you fancy
    new Request({
        url: "url_fetch.php",
        onComplete: function() {
            $("output").set("html", this.response.text);
        }
    }).send("url=http://www.foobar.com/about.php");
    
    PHP:
    the php would look something like this:
    <?
    
    // url_fetch.php
    function fetchURL($what) {
    
        // code the fetch through curl or fopen.
        ... 
        return $data;
    }
    
    echo fetchURL($_GET['url']);
    
    ?>
    PHP:
    you can apply additional formatting through the php, remove things like header, footer etc and extract only relevant data.

    also - keep in mind that relying on fetching this in real time will cause a severe CPU and network delay, especially so on larger sites. i would consider sending the fetch script to a cron job that runs say every 5 mins and serving the last fetched text from a database.

    good luck
     
    dimitar christoff, Oct 7, 2009 IP