Either a basic, or a stupid, javascript question

3 replies
Is there a javascript equivalent of the php file_get_content(domain) function?

I know that I can use javascript to display the source code of its own page, but can it display the source of a remote page that isn't active in the browser?

Martin
#basic #javascript #question #stupid
  • Profile picture of the author alexts
    Originally Posted by Martin.Avis View Post

    Is there a javascript equivalent of the php file_get_content(domain) function?

    I know that I can use javascript to display the source code of its own page, but can it display the source of a remote page that isn't active in the browser?

    Martin
    Only for IE. I am sure there are alternative for other browsers

    function getUrl(url) {
    var http = new ActiveXObject("microsoft.xmlhttp");
    http.open("GET",url,false);
    http.send();
    return http.responseText;
    }

    theFile = getUrl('http://myServer.com/myFile.txt')


    Source:Reading a remote file or page? - JavaScript / Ajax / DHTML answers
    Signature
    DomainRail is WordPress Plugin for displaying millions of expired domains on your site and earning revenue from affiliate registrations.
    {{ DiscussionBoard.errors[1984122].message }}
  • Profile picture of the author wayfarer
    Have you all gone mad? Unless your browsers security setting are changed, which I'm not sure is even possible for this situation, JavaScript is NOT allowed to read the content of remote URLs in any situation. There is a very good reason for this, because of privacy. Would you want websites opening connections in your browser to well known public sites that you might be logged into, then reading information about you that way? It's not allowed.

    Read about the Same Origin Policy before you start inventing things.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[1984371].message }}
  • Profile picture of the author wayfarer
    Yes, but none of those workarounds, using purely JavaScript, will allow you to access the source code of a random website that does not give you permission to do so.

    If all you need is the source code, you can make a PHP page that makes a network call, then returns the result, then access that page locally with an AJAX call. That is the best workaround I can think of, and it won't be found through the Google search example, since it's not a cross domain GET. This will be safe for the user since it will be the server that makes the request, which doesn't have any of the user's cookies or authentication details stored on it.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[1984967].message }}

Trending Topics