开发者

How to get page source of page at another domain (in Javascript!)

开发者 https://www.devze.com 2023-01-16 00:42 出处:网络
I am trying to get the source page of a webpage on a different domain. I kn开发者_如何学Cow this is easily done with PHP for example but I would like to do it in Javascript because I am getting result

I am trying to get the source page of a webpage on a different domain. I kn开发者_如何学Cow this is easily done with PHP for example but I would like to do it in Javascript because I am getting results from a page and if I use a server-side language, the original website will block the calls since they come from the same IP. However, if the calls are done on the client side, it is like the user request the results each time (different user, different IP, no original site blocking me). Is there a way to do that (even if not in Javascript but client-side).

To clarify the code I want will be applied to an HTML page so I can get the results, style them, add/delete, etc then display them to the user.

Thank you.


Modern browsers support cross-domain AJAX calls but the target site has to allow them by using special headers in the reply. Apart from that, there is no pure Javascript solution AFAIK.


Could you use an iframe? You wont have direct access to the markup to due to cross-domain restrictions, but you can still display the third-party page to the user...

window.onload = function() {
  var i = document.createElement("IFRAME");
  i.setAttribute("name", "ID_HERE");
  i.setAttribute("id", "ID_HERE");
  i.setAttribute("src", "URL_HERE");
  i.style.maxHeight = "300px";
  i.style.width = "100%";
  document.body.appendChild(i);
}


On Windows you can use HTA

An HTA can access anything crossdomain in an iframe for example

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号