Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
.contentDocument on object-tag returns null (sometimes)

My problem is that based on how I view the project I've built, objectRef.contentDocument sometimes returns a proper Document but other times null. This seems to be based on whether I serve the project from a webserver or simply open the HTML as file.

Let's get accustomed with the code though - what I'm trying to build is an image-carousel with SVG-icons that change color based on scroll position as navigation-buttons:

<div class="album-carousel">
    <div class="cover-container">
        <div class="cover"></div>
        ...
    </div>
    <object class="arrow left" data="assets/svg/arrow.svg" type="image/svg+xml"></object>
    <object class="arrow right" data="assets/svg/arrow.svg" type="image/svg+xml"></object>
</div>

When inspecting this in Chrome, the following actually gets rendered - as you see, a Document is created within the object-tag:

inspector content

I then get a reference to the album-carousel in JS and create a new instance of my own class, only after the DOM is fully loaded:

class AlbumCarousel {
  elementRef;
  arrowLeft;
  arrowRight;

  constructor(elementRef, scrollBy = 3) {
    this.elementRef = elementRef;
    this.arrowLeft = elementRef.children[1];
    this.arrowRight = elementRef.children[2];

    const scrollRight = () => {
      // irrelevant code
    }

    this.arrowRight.contentDocument.addEventListener("click", scrollRight);

    if (this.compoundWidth > Number.parseInt(getComputedStyle(this.albumContainerRef).width)) {
      this.arrowRight.contentDocument.children[0].style.fill = colors.base;
    }
  }

}

this.arrowRight.contentDocument.addEventListener(...) will throw an error when opened as file, however not when opened through IntelliJ's built-in local webserver - or am I searching in the wrong place here?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!