Javascript get page source from url
First, you must know that you will never be able to get the source code of a page that is not on the same domain as your page in javascript. (See http://en.wikipedia.org/wiki/Same_origin_policy). Show
In PHP, this is how you do it:
In javascript, there is three ways : Firstly, by XMLHttpRequest : http://jsfiddle.net/635YY/1/
Secondly, by iFrames : http://jsfiddle.net/XYjuX/1/
Thirdly, by jQuery : [http://jsfiddle.net/edggD/2/
]4 If for some odd reason, you wanted to view the source code of another page without having to actually browse to that page and click
“page view source,” you can use JavaScript to do so. In the example below, I use the “window.prompt()” function, to prompt the user for a web page whose source code they would like to see. That function returns the value of the input. We then use the “window.location” method to open up that URL in the browser, but we pre-pend the URL with ‘view-source:’, which achieves the same result as manually browsing to that page, and clicking: “page view source”. (Note:
As of this writing, this only works in Chrome and FireFox) As you can see, the second argument to the window.prompt() method is the default text that we want to appear in the text box. But what if the user accidentally deletes “http://” or simply ignores it, entering something like “google.com”? No problem. We first use the JavaScript “substring()” method to check to see if the first seven characters of the return value are: “http://”. If so, great; we just use the return value as is. If it is not, we pre-pend that value with “http://”, and we are all set. Example # 1:
SummaryThere may not be too many scenarios in which you need to do this, but it is handy to know now. You never know if, down the road, you might need some kind of variation on this functionality. Helpful Linkshttp://www.w3schools.com/jsref/obj_location.asp http://www.w3schools.com/jsref/met_win_prompt.asp http://www.w3schools.com/jsref/jsref_substring.asp Created: December-23, 2021
One can easily see the web page’s source code using browser dev tools. But the interesting feature that JavaScript provides us is that we can get the source code of a different webpage on our page without having to visit that page. This post shows various methods of achieving this. Use XMLHttpRequest() to Get HTML Code With a URLXML HTTP Request (XHR) mainly serves to retrieve data from a URL without refreshing the page. So they can be used to get the HTML code from a different page.
In the above example, we first make the HTTP object an HTTP request. Then we initialize and send the get request using Use jQuery to Get HTML Code With a URL
In the above example, we pass the Note The above solutions don’t work for cross-domain requests. Can JavaScript read the source of any web page?As a security measure, Javascript can't read files from different domains.
How do I find the SRC of a website?How to View Source Code. Firefox – CTRL + U (Meaning press the CTRL key on your keyboard and hold it down. While holding down the CTRL key, press the "u" key.) ... . Internet Explorer – CTRL + U. Or right click and select "View Source.". Chrome – CTRL + U. ... . Opera – CTRL + U.. How do I get the current page number in JavaScript?var lighboxHeight = (pagenumber-1)*window.
How do I get HTML using JavaScript?Get HTML elements by TagName: In javascript, getElementsByTagName() method is useful to access the HTML elements using the tag name. This method is the same as the getElementsByName method. Here, we are accessing the elements using the tag name instead of using the name of the element.
|