0

I want to load bing images pages "http://www.bing.com/images/search?q=home+design&qft=+filterui:imagesize-large&safe=off" , and save this url as html with javascript.

here is my code sample that I try :

function downloadURI(uri, name) {
  var link = document.createElement("a");
  link.download = name;
  link.href = uri;
  link.click();
}

but this code is send file to download on browser.

whether this may be done save url on server with javascript?

Suggestion and corrections are welcome.

Neha Thakur
  • 351
  • 1
  • 12
  • 37
  • JavaScript is a client-side language. You shouldn't download something using user's browser, and then upload it to server. – Yeldar Kurmangaliyev Dec 25 '15 at 05:41
  • @YeldarKurmangaliyev can you give me example code ? – Fajar Fariyanto Dec 25 '15 at 05:44
  • 2
    @FajarFariyanto He's saying that your strategy is likely wrong. Why don't you give us more details on what result you want to acchieve? – Pablo Dec 25 '15 at 05:45
  • @Pablo I want to get data from bing search, without having to overload the server. – Fajar Fariyanto Dec 25 '15 at 05:51
  • You will not be able to do it because of the [same-origin policy](https://en.wikipedia.org/wiki/Same-origin_policy). – Pablo Dec 25 '15 at 05:59
  • @Pablo if i'm use Access-Control-Allow-Origin: * – Fajar Fariyanto Dec 25 '15 at 06:01
  • @FajarFariyanto No, you got it backwards. Bing would have to say they allow you to access their website by using the header. For more details, refer to this question: http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work – Pablo Dec 25 '15 at 06:04

1 Answers1

0

One way to do this is with iframes, although it might not always be available for every site. But Bing seems to be ok with using iframes. I just tried adding the specific URL you listed into an iframe with the following HTML code and it worked:

<iframe src="http://www.bing.com/images/search?q=home+design&qft=+filterui:imagesize-large&safe=off"></iframe>

For other sites, such as Google, it might not be so easy. Google doesn't allow anything to be loaded in iframes by default, however it seems they will allow specific searches to be configured in your google account and display in iframes on sites you specify.

To achieve with Javascript (since the question mentions javascript) you can just leave the "src" attribute blank and set it using the script, for example if you only have one iframe on the page

document.getElementsByTagName('iframe')[0].src="http://www.bing.com/";
voidlike
  • 11
  • 3
  • can store iframe output maybe as cookie on browser? – Fajar Fariyanto Dec 25 '15 at 06:13
  • Why would you need it to be saved? With iframes each client will perform their own bing search so your server doesn't have to do anything. Anything saved as a cookie will be sent to your server by the clients so using cookies would be very wasteful. – voidlike Dec 25 '15 at 06:20
  • because on other page i'll parse this with php, i'm can parse it from cookies – Fajar Fariyanto Dec 25 '15 at 14:59