Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JQuery to Access a New Window's DOM

I am creating a new window that will contain text that user will print. I would like to do something similar to this:

var new_win = window.open();
$(new_win.document).html("Test");
like image 869
jdw Avatar asked Oct 17 '11 01:10

jdw


People also ask

Does jQuery manipulate the DOM?

Projects In JavaScript & JQueryjQuery provides a number of methods to manipulate DOM in efficient way. You do not need to write big and complex code to set or get the content of any HTML element.

Is jQuery better than DOM?

Pure JavaScript can be faster for DOM selection/manipulation than jQuery as JavaScript is directly processed by the browser. jQuery has to be converted into JavaScript to make it run in a browser. All these can be done in JavaScript but we may have to write many lines of code.

What does $( document .ready function () mean?

$( document ). ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

What is $( window in jQuery?

$(window). Use $(window).load(), when you want the code inside it to run only once the entire page is ready (not only DOM). It executes when the page is fully loaded, including frames, objects and images. Note: The load() method deprecated in jQuery version 1.8.


2 Answers

In this case you are trying to access a dom wich has no jQuery enhancement. You need to first load a jquery.js into this document. If done the syntax would be.

var popup = window.open('/some/url.html');
popup.document.$('body').html('test');

But be very careful, in multi document management and communication are many bugs and inconveniences between the many different browser versions and vendors.

It would really be better if you keep communication to a absolute minimum, and just load another complete html file into the popup.

like image 168
FloydThreepwood Avatar answered Oct 17 '22 21:10

FloydThreepwood


In our MVC Web application, We need to open a new tab and present a demo from a product. For this purpose, We need To apply some limitation in demo mode and click a button to play a video. we did it like this:

DemoTab = window.open("someUrl/demo", "_blank");
if (DemoTab) {                                
    $(DemoTab.document).ready(function () {        
        $("#hideInDemo", DemoTab.document).removeClass('floatContainer');
        $("#hideInDemo", DemoTab.document).css('display', 'none');
        $("#play", DemoTab.document).trigger('click');
    }); 
}

it only worked on Chrome and Edge.

So we solved the problem by action controller whitch called to load the page. We just passed a parameter by ViewBag to view and on document ready we checked the parameter so then we knew that it called for demo. So we applied our limitations.

like image 26
Ali Tabandeh Avatar answered Oct 17 '22 19:10

Ali Tabandeh