1

I don't know if it's possible but what I want is creating some script that scans into a webpage that isn't on my server and grab it's sourcecode so I could use some data, like PERL or Rails scripting but with Jquery/Javascript

Is that possible?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
steps
  • 774
  • 2
  • 16
  • 38
  • 1
    When you say sourcecode, do you mean the HTML response or the actual server side code? The latter is not possible (unless the web server is setup to allow it, which will be *very, very, very* rare). – Justin Helgerson Apr 17 '12 at 16:32
  • I need only the HTML response! – steps Apr 17 '12 at 16:33
  • to only way to do cross-site data in javascript is JSONP so unless the host supports it you're out of luck – scibuff Apr 17 '12 at 16:33
  • Cross Domain requests are not allowed in JavaScript without JSONP or some sort of locally-hosted proxy. A simple google search would have confirmed this. – mellamokb Apr 17 '12 at 16:33
  • AJAX request to your server, your server gets the source code and returns it. Job done! – musefan Apr 17 '12 at 16:34
  • Yes it could do problem you may have solution [here][1] [1]: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – Adil Apr 17 '12 at 16:35
  • I don't have a server, my script is a bookmarklet and that's the problem! I need to get a website HTML response without any server integration – steps Apr 17 '12 at 16:36
  • you may find solution to the problem here [here][1] [1]: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – Adil Apr 17 '12 at 16:36
  • There may be situations comes where you need to get html response on clients. for this you need to utilize java applets on client machine. – irfanmcsd Apr 17 '12 at 16:39

4 Answers4

3

In the general case, no, it's not possible from the client because of the Same Origin Policy, which prevents your loading and looking at the HTML and script source of content loaded from different origins. (You can, of course, do this with JavaScript on the server — NodeJS, Rhino, and similar.)

However, if the other domains support Cross-Origin Resource Sharing and the browser you're using also supports it, that can allow the other end (the other website) to allow code from your origin (or all origins) to access it. The other end has to allow it, though.

You'll also hear about JSON-P in this context, but that only allows you to get data (not HTML source and such) from the other end, and requires that the other end explicitly support it.

So you probably have to have a server involved. It doesn't necessarily follow that it has to be your server, though: You can use YQL as a cross-domain proxy for some purposes.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

No its not possible from clientside to grab HTML code from another webpages. Unless you are able to configurate the other servers.

http://en.wikipedia.org/wiki/Same_origin_policy For more reading

Simon Edström
  • 6,461
  • 7
  • 32
  • 52
0

Same-Origin Policy

The same-origin policy is a sweeping security restriction on what web content JavaScript code can interact with. It typically comes into play when a web page includes elements or opens other browser windows. In this case, the same-origin policy governs the interactions of JavaScript code in one window or frame with the content of other windows and frames. Specifically, a script can read only the properties of windows and documents that have the same origin as the document that contains the script.

The origin of a document is defined as the protocol, host, and port of the URL from which the document was loaded. Documents loaded from different web servers have different origins. Documents loaded through different ports of the same host have dif- ferent origins. And a document loaded with the http: protocol has a different origin than one loaded with the https: protocol, even if they come from the same web server.

It is important to understand that the origin of the script itself is not relevant to the same-origin policy: what matters is the origin of the document in which the script is embedded. Suppose, for example, that a script hosted by host A is included (using the src property of a element) in a web page served by host B. The origin of that script is host B and the script has full access to the content of the document that contains it. If the script opens a new window and loads a second document from host B, the script also has full access to the content of that second document. But if the script opens a third window and loads a document from host C (or even one from host A) into it, the same-origin policy comes into effect and prevents the script from accessing this document.

author: David Flangan: Javascript; The Definite Guide

Ram
  • 143,282
  • 16
  • 168
  • 197
0

In the purest sense of your question, yes this is possible but I get the feeling you're misunderstanding how these technologies work. Even if x-domain requests were possible there is literally no way for javascript to gather data from an external source except through AJAX when being used inside a browser. The reason I say the answer is yes however is because these restrictions only apply when javascript is being executed inside a browser. If you're using javascript in conjunction with say node.js for example the rules are completely different as javascript is capable of creating sockets and the x-domain restrictions no longer apply.

Perhaps you should clarify your question so a more appropriate answer may be offered.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147