Skip to content Skip to sidebar Skip to footer

Print Contents Of HTML Object Tag With JavaScript

I am currently trying to create a method that calls the browser's print function to print the document content embedded within my page. D3.js and jQuery libraries are included alre

Solution 1:

The window.open() function can take another parameter, a window name. First, specify a name for your current window, like window.name="persists"; and then, for the popup you could specify something like Popup=window.open(URL, "popped"); This will ensure that the just-opened URL doesn't replace the code you are running, and the Popup variable gives you access to anything you want in that popped window --use Popup.document to access the document object, for example.

I'm going to add this from a comment I made below. Think of your code as being divided into two different functions. One holds the window.open() instruction, and the other --let's call it "manipulator()"-- holds the code to access the document object and do things with that data. In many cases a browser doesn't immediately do something that JavaScript tells it to do; it waits for the currently-running JavaScript function to end, before doing it. That means you need a way of connecting the first function to the second, and the simplest way to do that is through setTimeout(). For example right after calling window.open() you could specify setTimeout("manipulator();", 250);, and then do a return from that first function.

The manipulator() function would be called 1/4 second afterward, but before it runs, the browser will have performed the window.open() operation. So, when the manipulator() function runs, inside it you will be able to access the document object of the just-opened window, and do things with that data. (Do note that this sort of thing, dividing work between different functions, is a major reason why global variables are extremely useful in JavaScript --"Popup" should be a global here.)


Post a Comment for "Print Contents Of HTML Object Tag With JavaScript"