Monday, March 30, 2015

K2 SmartForms - Close all the child windows on closing parent window using JavaScript

Requirement is to close all those windows that opens from parent window on close of parent window. we are not using Modal dialog window here.


we will use java script to achieve this as below.


1. In the parent form/view place a datalabel and place the expression below as new expression:


Use the button tooltip to fire on click event in client side and open the new window, store the window open in variable and close the variable beforeunload event of the form/window.


This will make sure you are closing the window before routing .

<script type="text/javascript">$(document).ready(function() { var winVar=null;  $(window).bind("beforeunload", function(){ if(winVar != null){winVar.close();} }); $("[title='TTAction Button']").click(function() { var urlText= document.getElementsByName("dtlDocumentumURL")[0].innerHTML; winVar= window.open(urlText, '_blank', 'modal=yes,toolbar=0,location=0,menubar=0');});});</script>




If you want more details about javascript expression in DataLabel, browse below link


http://blog.velocity-it.com/2014/10/07/how-to-call-jquery-or-javascript-code-in-a-k2-smartform/comment-page-1/


In IE the window.close () does not work, if there is a pdf downloaded in popup.


So use the below code for doing the same


<script type="text/javascript">$(document).ready(function() { var winVar=null;function openPopup(link) {  var html = "<html><head><title></title>"; html += "</head><body style='margin: 0;'>"; html += "<iframe height='100%' width='100%' src='" + link +"'></iframe>";  html += "</body></html>";   win = window.open("", "_blank", "resizable=1,status=0,toolbar=0,menubar=0");    win.document.write(html);    return win;}
  window.onbeforeunload=function() { if(winVar != null){  winVar.close();} }; $("[title='TTAction Button']").click(function() { var urlText= document.getElementsByName("dtlDocumentumURL")[0].innerHTML; winVar=openPopup(urlText);}); });</script>