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

582
Views
JQuery .contents() on an iframe displaying base64-encoded pdf

I have an iframe displaying a base64-encoded PDF. Once it has loaded, I attempt to do .contents().find(..), however it is not finding the contents. Within the iframe is the correctly-loaded HTML doc containing the PDF, and a text element " ". Doing .contents() will return one item, which is the text " ".

All of this code works fine if I don't include the base64 string as source for the iframe (data:application/pdf;base64,..). The PDF itself displays fine and is loaded correctly, but I cannot find a way to access the document through JS / JQuery. I do not believe this is a timing issue, as in debugger mode when paused at the line before it fails, I can see the content has been loaded fully (furthermore, the .on('load', ..) method is being used).

Relevant code (ASP .NET MVC):

HTML:

<iframe class="converted-display"  id="doc-iframe" src="@ViewBag.RAWData">
</iframe>

JS:

$(document).ready(function () {

    var url = window.location.href

    if (url.toLowerCase().includes("convert")) {
        $('#doc-iframe').on('load', function () {
            debugger
            $('#doc-iframe').contents().find('body').addClass('..')

            var element = $('#doc-iframe').contents().find('head').first()[0];

            var style = document.createElement('style');

            style.innerHTML = ..

            window.styleNode = style;

            element.parentNode.insertBefore(style, element);
        })
    }

})

ViewBag.RAWData will contain a (data:application/pdf;base64,..) string.

The result of $('#doc-iframe') when paused at the debugger, clearly showing that the doc has loaded

Showing the result of .contents() and attempting to index it

To rule out async issues, I also tried the commands in the console after the page had fully loaded, and met the same problems.

Does anyone have any suggestions or resources? I have read the doc for .contents() and various other docs, have also tried a multitude of variations on the JS, as well as different methods of displaying PDFs e.g. object, embed, which is making me wonder if the root issue is the src which is being provided.

Thank you very much for any help.

UPDATE:

Cross-origin issue, which I did not expect since the data is essentially hard-coded at the point that it has been loaded - any suggestions for how to fix this?

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!