Skip to content Skip to sidebar Skip to footer

Get Html Source Code

I am trying to put the html source code for any webpage in a string using Javascript. Please tell me if i can do something else to solve my problem.. I am using the following code

Solution 1:

I am trying to put the html source code for any webpage in a string using Javascript

If by "any" you mean pages from origins other than the origin your document is served from, you can't do that from JavaScript running in a browser, because you're using an ajax call and those are restricted by the Same Origin Policy, which says that (for instance) script running in a document on http://stackoverflow.com can't use ajax to load content from http://example.com. (An "origin" is more than just the domain name, there are several aspects to it, see the link for details).

Some of the pages you might request (but probably very few) might support Cross-Origin Resource Sharing, in which case if they allow your origin (probably by allowing all origins), you could use ajax to load their content.

If you're running JavaScript outside the browser (NodeJS, SilkJS, RingoJS, Rhino, Windows Scripting Host, etc.), then the SOP wouldn't apply, but I suspect you'd probably need to use something other than the XMLHttpRequest object to do it.

But fundamentally, in a web page (not an extension/add-on) in a browser, you can't do that.

...but i always get the ... source code for "PAGE NOT FOUND" page

But that sounds like the URL is just wrong.


Post a Comment for "Get Html Source Code"