Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:21:38+00:00 2026-05-27T07:21:38+00:00

I have basically the opposite problem to this question: How to prevent iframe from

  • 0

I have basically the opposite problem to this question: How to prevent iframe from loading when injected into the DOM?
I wish to load an IFRAME element which is not injected into the DOM.

I am attempting to print a page via javascript. The goal is to avoid having to navigate a link, issue a print command, and navigate back.

I have jQuery available to me.

// fake table that illustrates pertanent DOM
<table class=diary><tr><td data-job=12345 contextmenu=job_menu><a href=/jobs/12345>view job 12345</a></table>

// real code
<menu type="context" id="job_menu">
    <menuitem label="Print Job" onclick="PrintJob(); return false;"></menuitem>
</menu>
<script type="text/javascript">
    var target = null;

    function PrintJob()
    {
        var job_no = $(target).data('job');
        if(job_no > 0) {
            $('<iframe name="printjob" src="/jobs/'+job_no+'">').load(function () {
                this.focus();
                this.print();
            })
            .trigger('load');
        }
    }

    $('.diary').bind('contextmenu', function (ev) {
        var td = ev.target;
        while(td && td.nodeName != 'TD')
            td = td.parentNode;
        target = td;
    });
</script>

The problem is that calling .trigger('load') on the generated IFRAME element does not cause a GET request on the src attribute URL. I was hoping the default handler would be called first, then my attached handler would fire, and print the contents of the iframe, but my load handler just gets called immediatly.

I can’t attach the print call to the iframe’s contentDocument.onready event because contentDocument is nil until the frame has loaded. I don’t know if you can focus an element not in the DOM or if that is necessary for printing, but the code I have seen elsewhere all had .focus() preceeding .print()
(I also tried using window.frames['printjob'].print(), but I suspect window.frames['printjob'] is nil if the iframe is not yet in the window 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:21:38+00:00Added an answer on May 27, 2026 at 7:21 am

    As Tom van der Woerdt said, the iframe won’t load unless you add it to the DOM.

    But that doesn’t mean it has to be visible:¹

    $(`<iframe name="printjob" src="/jobs/'+job_no+'" style="display: none">`)
        .load(function() {
            this.contentWindow.focus();
            this.contentWindow.print();
        })
        .appendTo(document.body);
    

    Note the display: none. In my experiments with modern browsers (and those of user2027290, who pointed this out), the iframe gets loaded, and the print dialog shown.

    The trick is figuring out when to remove it. Modern browsers have an afterprint event you could use. Or you could just remove it prior to the next time the user prints, avoiding having more than one of them.


    ¹ In the 2011 version of this answer, I had it loaded visibly but outside the viewport, like this:

    $('<iframe name="printjob" src="/jobs/'+job_no+'" style="position: absolute; left: -10000px; top: 0">')
        .load(function() {
            this.contentWindow.focus();
            this.contentWindow.print();
        })
        .appendTo(document.body);
    

    I don’t recall whether I did that because display: none didn’t work at the time or because I just didn’t think of it, but if you run into any issue with the display: none solution, that might be a fallback to try.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have basically the same problem outlined in this question, however I am using
Well, this is the exact opposite problem that I normally have with Javascript synchronicity
I basically have something like this: void Foo(Type ty) { var result = serializer.Deserialize<ty>(inputContent);
I basically have three tables, posts, images and postimages (this simply contains the ids
Basically I want to do the opposite of what this guy did... hehe. Python
I have basically MySQL dump from a WordPress database with some data. The whole
Basically, I have an iframe embedded in a page and the iframe has some
I have what seems to be the exact opposite of everyone else's problem! I
Maybe some of you could have ran into the same problem i did. Imagine
Been trouble-shooting this for a few days now and have basically run dry of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.