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

Hello,

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 4 months ago
This is a tough thing: Browsers have security policies that prevent simply taking content from other websites.
jduplessis294 4 months 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 4 months 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 4 months ago
Definitely. Hey, I'll look around for something like that. No problem, I appreciate it! Cheers :)
sharper 4 months 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:
https://codepen.io/kostasx/pen/XYrgoa?editors=1010

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 11 days ago