{"id":322,"date":"2013-10-21T22:45:54","date_gmt":"2013-10-21T14:45:54","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=322"},"modified":"2018-09-04T13:10:54","modified_gmt":"2018-09-04T05:10:54","slug":"how-to-use-jquery-to-access-the-dom-structure-of-parent-html-webpage-from-within-the-child-html-window","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-access-the-dom-structure-of-parent-html-webpage-from-within-the-child-html-window\/","title":{"rendered":"How to use jQuery to access the DOM structure of parent HTML webpage from within the child HTML window"},"content":{"rendered":"<p>Sometimes, instead of showing a dialog box within a web page, it is more strategic to spawn a new HTML window as a dialog box to capture user input. This technique is especially useful when there are Java applets embedded in the web page, as Java applets tends to appear in front of every visual element on a HTML web page. There is no easy solution to place a visual element, for instance a HTML div, on top of Java applets.<\/p>\n<p>Spawning another web page in another window is straightforward, but how do I send data collected from a child HTML window back to the parent HTML webpage? With this question in mind, I set out to find an answer.<\/p>\n<h2>A sample scenario<\/h2>\n<p>I will have a parent web page which contains a link for me to launch a child web page as a separate browser window. Within the child web page, I have a text field and a submit button for users to write messages back onto the parent web page.<\/p>\n<h2>The parent web page<\/h2>\n<p>Once I had created the sample scenario, I proceeded with building the parent web page.<\/p>\n<p>With my <a href=\"http:\/\/www.techcoil.com\/blog\/code-segments-for-rendering-html-4-0-pages\/\" title=\"Code segments for rendering html 4.0 pages\" target=\"_blank\">HTML code reference<\/a> and <a href=\"http:\/\/www.techcoil.com\/blog\/handy-javascript-code-segments\/\" title=\"Handy Javascript code segments\" target=\"_blank\">JavaScript code reference<\/a>, I created the following HTML document:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\t&lt;title&gt;How to use jQuery to access the DOM structure of parent HTML webpage from within the child HTML window&lt;\/title&gt;\r\n\t&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n\t&lt;script type=&quot;text\/javascript&quot;&gt;\r\n\t\r\n\t\t$(document).ready(\r\n\t\t\tfunction () {\r\n\t\t\t\t$('#linkToSpawnChildWebPage').click(\r\n\t\t\t\t\t\r\n\t\t\t\t\tfunction (event) {\r\n\t\t\t\t\t\tevent.preventDefault();\r\n\t\t\t\t\t\twindow.open('http:\/\/www.techcoil.com\/proof-of-concepts\/adding-html-components-to-parent-web-page-dynamically-with-jquery','_blank','height=600,width=1024, status=yes,toolbar=no,menubar=no,location=no'); \r\n\t\t\t\t\t}\r\n\t\t\t\t);\r\n\t\t\t}\r\n\t\t);\r\n\t\r\n\t&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\t&lt;a id=&quot;linkToSpawnChildWebPage&quot; class=&quot;actionLink&quot;&gt;Spawn child web page&lt;\/a&gt;\r\n\t\r\n\t&lt;h3&gt;Messages from child web page&lt;\/h3&gt;\r\n\t&lt;div id=&quot;messagesArea&quot;&gt;\r\n\t&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>At the head section of the parent web page, I reference a jQuery library hosted by Google. By doing so, I attach a jQuery object to the web page's <code>window<\/code> object. In the JavaScript block that follows, I look for a HTML element with <code>linkToSpawnChildWebPage<\/code> as its id and attach a callback function for jQuery to notify my codes when the HTML element is being clicked. When that happens, the function is given an <code>event<\/code> object which I use for stopping <code>linkToSpawnChildWebPage<\/code>'s default behaviour when it is being clicked on. <\/p>\n<p>After calling <code>event.preventDefault<\/code>, I use the <code>window<\/code> object of the parent web page to launch the child web page in a window of its own.<\/p>\n<p>In the body section of the parent web page, I show<\/p>\n<ul>\n<li>a link element with id as <code>linkToSpawnChildWebPage<\/code>,<\/li>\n<li>a header<\/li>\n<li>and a div with <code>messagesArea<\/code> as its id to contain messages from the child web page<\/li>\n<\/ul>\n<p>With the parent web page in place, I set out to build the child web page.<\/p>\n<h2>Getting a reference to the jQuery object in the parent web page from the child web page<\/h2>\n<p>In order for my child web page to access the DOM of the parent web page, I first write the JavaScript codes to get the <code>window<\/code> object of the parent web page.  <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar parentWindow = window.opener;\r\n<\/pre>\n<p>A call to <code>window.opener<\/code> from the child web page returns the <code>window<\/code> object of the parent web page that launches it. I define a variable, <code>parentWindow<\/code>, to give <code>window.opener<\/code> a easy-to-remember alias. <\/p>\n<h2>Sending the message from child web page to parent web page<\/h2>\n<p>With a variable that points to the <code>window<\/code> object of the parent web page, I proceed with writing the codes to send the message from the child web page to the parent web page:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction addMessageToWebPage(msg) {\r\n    \r\n    $messagesArea = parentWindow.$('#messagesArea');\r\n    $messagesArea.html('&lt;p&gt;' + msg + '&lt;\/p&gt;' + $messagesArea.html());\r\n\t\r\n}\r\n\t\t\r\n$(document).ready(\r\n\tfunction() {\t\t\t\t\r\n\t\tif (!parentWindow) {\t\r\n\t\t    alert('Unable to get a reference to parent window.');\t\t\r\n\t\t} \r\n\t\telse {\r\n\t\t\t$('#messageForm').submit(\t\t\t\t\r\n\t\t\t\tfunction(event) {\r\n\t\t\t\t\taddMessageToWebPage($('#messageTextField').val());\r\n\t\t\t\t\tevent.preventDefault();\r\n\t\t\t\t}\r\n\t\t\t\t\t\t\t\r\n\t\t\t);\r\n\t\t}\t\t\r\n\t}\r\n);\r\n<\/pre>\n<p>I first define <code>addMessageToWebPage<\/code> which accepts a message from its caller. As the jQuery object is attached to the parent's window object, I call <code>parentWindow.$<\/code> to query the DOM of the parent web page. I send a query to <code>parentWindow.$<\/code> to look for the HTML element with <code>messagesArea<\/code> as its id, and remember it as <code>$messagesArea<\/code>. I then encapsulate the message from the caller with a HTML paragraph tag and append the result before the existing HTML content.<\/p>\n<p>I then call <code>$(document).ready<\/code> and give it a function to prepare the child web page for interaction with the user. I first check whether <code>parentWindow<\/code> is successfully defined in the earlier JavaScript segment. <\/p>\n<p>If it is not defined, it will mean that this web page is not open by another web page. I alert the user that the reference to the parent web page cannot be retrieved. If I have a reference to the <code>window<\/code> object of the parent web page, I attach a callback function to <a href=\"http:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-intercept-form-submissions-and-validate-form-input\/\" title=\"How to use jQuery to intercept form submissions and validate form input\" target=\"_blank\">intercept form submission events<\/a> from the HTML form with <code>messageForm<\/code> as its id.<\/p>\n<p>In the given callback function, I call <code>addMessageToWebPage<\/code>, giving it the value found in the HTML text field with <code>messageTextField<\/code> as its id. I then stop the form submission with a call to <code>event.preventDefault<\/code>.<\/p>\n<h2>The child web page<\/h2>\n<p>For the view portion of the child web page, I reuse the same form from <a href=\"http:\/\/www.techcoil.com\/blog\/how-to-add-html-component-dynamically-with-jquery\/\" title=\"How to add HTML components dynamically with jQuery\" target=\"_blank\">the proof of concept on using jQuery to add HTML components to a web page dynamically<\/a>. <\/p>\n<p>I add in the JavaScript codes mentioned earlier to the form and I get the following HTML document:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;Adding HTML components to parent web page dynamically with jQuery&lt;\/title&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n\r\nvar parentWindow = window.opener;\r\n\t\r\nfunction addMessageToWebPage(msg) {\r\n    \r\n    $messageArea = parentWindow.$('#messagesArea');\r\n\t$messageArea.html('&lt;p&gt;' + msg + '&lt;\/p&gt;' + $messageArea.html());\r\n\t\r\n}\r\n\t\t\r\n$(document).ready(\r\n\tfunction() {\r\n\t\t\t\t\t\t\r\n\t\tif (!parentWindow) {\t\r\n\t\t    alert('Unable to get a reference to parent window.');\t\t\r\n\t\t} \r\n\t\telse {\r\n\t\t\t$('#messageForm').submit(\t\t\t\t\r\n\t\t\t\tfunction(event) {\r\n\t\t\t\t\taddMessageToWebPage($('#messageTextField').val());\r\n\t\t\t\t\tevent.preventDefault();\r\n\t\t\t\t}\r\n\t\t\t\t\t\t\t\r\n\t\t\t);\r\n\t\t}\t\t\r\n\t}\r\n);\r\n\t\t\t\t\t\t\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\t&lt;form id=&quot;messageForm&quot;&gt;\r\n\t&lt;fieldset&gt;\r\n\t    &lt;label for=&quot;messageTextField&quot;&gt;Message to add:&lt;\/label&gt;\r\n\t    &lt;input id=&quot;messageTextField&quot; name=&quot;messageTextField&quot; type=&quot;text&quot;&gt;\r\n\t&lt;\/fieldset&gt;\r\n\t&lt;fieldset&gt;\r\n\t        &lt;input name=&quot;submitButton&quot; type=&quot;submit&quot; value=&quot;submit&quot;&gt;\r\n\t&lt;\/fieldset&gt;\r\n\t&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<div class=\"callToAction\">\n   <a target=\"_blank\" href=\"http:\/\/www.techcoil.com\/proof-of-concepts\/using-jquery-to-access-the-dom-structure-of-parent-html-webpage-from-within-the-child-html-window\" class=\"readMoreLink\">See how this proof of concept works<\/a>\n<\/div>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-5c\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5c&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5c&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F322&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Sometimes, instead of showing a dialog box within a web page, it is more strategic to spawn a new HTML window as a dialog box to capture user input. This technique is especially useful when there are Java applets embedded in the web page, as Java applets tends to appear in front of every visual element on a HTML web page. There is no easy solution to place a visual element, for instance a HTML div, on top of Java applets.<\/p>\n<p>Spawning another web page in another window is straightforward, but how do I send data collected from a child HTML window back to the parent HTML webpage? With this question in mind, I set out to find an answer.<\/p>\n","protected":false},"author":1,"featured_media":1208,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[16,89],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/jquery-logo-mark-dark.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-5c","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/322"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=322"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/322\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1208"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}