Pulling in a DIV from another website
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x


I'm having an issue with pulling in the contents of another site. For example's sake, I'm using dilbert.com to pull in one of my favorite comics. I thought that using getElementsByClassName would do the trick, but it doesn't seem to want to work. Is this possible? What might I be doing wrong? Here's my code that I'm tinkering with.

The goal is to embed the day's comic into the page.

Thanks in advance.

Hi, getElementsByClassName returns an array, so in order to get a result you should loop or get the first, e.g. comics[0]. But with dilbert.com it will not work because it returns this header: X-Frame-Options: SAMEORIGIN Read this for more information:
cueball over 3 years ago
This is a tough thing: Browsers have security policies that prevent simply taking content from other websites.
jduplessis294 over 3 years ago
Hey! Fair enough. Some more context: I have no intentions to do anything commercial with this, I basically have a small local app and just thought it'd be neat to embed the strip. Glad to know though, thank you. :)
sharper over 3 years ago
Of course. Even without any bad intentions, sometimes annoying browsers get in the way :) Best way to do what you want is to use an API, but I don't see anything really. An API would allow you to access Dilbert comics, possibly for a fee, but from any webpage. Sorry I can't be of much service on this one.
jduplessis294 over 3 years ago
Definitely. Hey, I'll look around for something like that. No problem, I appreciate it! Cheers :)
sharper over 3 years ago

Crowdsource coding tasks.

1 Solution

Winning solution

You can use a CORS fetch service such as: https://cors.now.sh/
Here's a demo:

I noticed that this was working but isn't now. Is it for you? I wonder what's changed? Edit: Nevermind, looks like maybe the cors service is just down.
sharper over 3 years ago