Get request to refresh page?

Hey everyone.

With Javascript, is it possible to make a get request to another page that is outside of your domain, so for example google.com, and make a request to refresh that page every 5 minutes?

Hello @alex067
Yes and no.

Modern browsers have implemented a so-called “Cross-Origin-Resource-Sharing” Policy.
That means that servers can define origins (hosts/webservers/ips/domains) who is allowed to dynamically fetch which contents. Read more about that here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

That information is in the header of the response.
A header is information sent with the request or with the response.

Example:

Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET
Access-Control-Allow-Headers

Now if you have two servers that you control, you can absolutely make these requests from the front-end without a problem.

This barrier only exists for the browser though. If your server asks for the same resource, you will be able to get it.

So for your example, the code would look somewhat like this:

Just that codepen restricts the way how other content online can be accessed (you will get something like

pen.js:2 Uncaught DOMException: Blocked a frame with origin "https://s.codepen.io" from accessing a cross-origin frame.
    at pen.js:14:57

For your website - it should work.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.