Link to home
Start Free TrialLog in
Avatar of paulgn
paulgnFlag for United States of America

asked on

How to save a window reference from a javascript window.open function into the session

Hi
    I am having trouble implementing a context-sensitive pop-up help window using the javascript method called window.open.  Although I have the basic pop-up window working just fine, Im having a lot of trouble implementing a customer request that I was given yesterday.  Im stuck and I would really appreciate it if you could help me. Listed below are the steps Ive taken, together with my code samples and questions.  

   Currently, I can successfully navigate to any one of our applications 68 UI windows and click the Help menu item on our applications top tool bar.  This Help menu item is in a jsp called pageHeader.jsp.  Each of our 68 UI windows includes this pageheader.jsp by entering the following statement at the top of their jsp:

<%@ include file=pageheader.jsp%>

Within the pageheader.jsp, Ive written code so that when the Help menu item is clicked, I execute a javascript function to open the pop up help window.  Here is the existing code in my JSP page for the Help menu item:

<td style="background-color: #fff;background-image: url(images/topbar_bg.jpg);background-repeat: repeat-x;margin: 0;padding: 0;">
     <div style="vertical-align: middle;cursor: help;padding: 0 4px 0 4px;white-space: nowrap;"  onClick="javascript:open_ContextSensitiveHelpWindow('<%=tagName%>');">
      <strong class="newtxt">Help</strong></div>
</td>
   
Here is my javascript function called open_ContextSensitiveHelpWindow

<script type="text/javascript">
function open_ContextSensitiveHelpWindow(tagName)
{
window.open("help/help.html?context=help&amp;topic="+tagName+"&amp","help_win","menubar=0,resizable=1,width=700,height=350,addressbar=0,scrollbars=1,left=275,top=275");
}
</script>

As I mentioned above, this code works fine.  Unfortunately, if, after I click the Help menu item and pop up the Help window, I then decide to switch to another UI screen, I can click the Help menu item again and a second pop up window will appear.  In fact, even if I stay on the current UI window and click Help, another pop up window will appear.  The customer has requested that only 1 pop up window appear.  Once this window is open, then the customer can switch to any of the 68 UI windows and click Help, and then only the contents of the existing Help window will change (I actually got the contents to change during an experiment by coding window.location.href = newURL.)  But the customer does not want multiple pop up windows to appear.
Despite my research and experiments, Im unable to implement this request.  The only approach Ive tried is to save a reference to the pop up help window in a hidden field on the pageheader.jsp.  I was thinking that if I could save the window reference to the hidden field, then perhaps I could store it in the users session where it would be available each time the pageheader.jsp is loaded. The code below works if I stay on the samd UI application window.  But if I switch UI windows, then I lose the reference to my pop-up window.  Can you tell me if its possible to save the window reference in the session (or request/response objects) from my hidden field?   If yes, am I taking the correct approach?  What more do I need to do to get the window reference into the session?  If not, is there a different approach I can use to implement the customers request?

Here is my experimental code:

I call the following function to initialize the hidden input value during the loading of pagehelp.jsp

<body onload="javascript: doit();">

Here is my hidden field:

<td><input type="hidden" id="popupWin" name="popupWin"/></td>

Here are my javascript functions

<script type="text/javascript">
function open_ContextSensitiveHelpWindow(tagName)
{
  win = document.getElementById("popupWin").value;
  if(typeof(win) == undefined")
  {
win=window.open("help/help.html?context=help&amp;topic="+tagName+"&amp","help_win","resizable=1,width=700,height=350,scrollbars=1,left=275,top=275");
    document.getElementById("popupWin").value = win;
  }
  else if (win.closed)
  {
win=window.open("help/help.html?context=help&amp;topic="+tagName+"&amp","help_win","resizable=1,width=700,height=350,scrollbars=1,left=275,top=275");  
  }
  else
  {
   // Get the existing URL in the popup window and change the topic (experiment to test    
   //  changing topics within the window
    var url = win.location.href;
    var topicIndex = url.indexOf("topic=");
    var suburl = url.substring(0, topicIndex);
    var newurl = suburl + "topic=main_search&";
    win.location.href = newurl;
  }
  win.focus();
}  
function doit()
{
   var test = 99;
   document.getElementById("popupWin").value = test;
 }

One last question, Ive noticed that every time I save the window reference to the hidden field, when I retrieve it, the javascript typeof always says it is a string.  Upon calling the window.open function a second time, I can see that the window reference is a  [object], but the typeof keeps telling me string.  Does that value= field on the hidden field always report values of type string?

Thank you for your help.
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

All form field values are always only String.

And your problem should be solved by using always the same windowName for all popups. The window name parameter ensures that the opened window with same name is reused.

Therefore this should work:
<script type="text/javascript">
function open_ContextSensitiveHelpWindow(tagName){
  helpWin = window.open("help/help.html?context=help&topic="+tagName+"&","helpwin","resizable=1,width=700,height=350,scrollbars=1,left=275,top=275");
  helpWin.focus();
}
</script>

Open in new window

Avatar of paulgn

ASKER

Thank you for your proposed solution, but it does not work.  
I did try this solution prior to writing you because I read that using the same window name will prevent opening a second window.  But for reasons I do not understand, a second help window still opens if I click the "Help" link a second time while I am still on the same UI page.
However, just to be sure, I did paste your solution into my code and executed it. But I got the same result of multipe pop up help windows.
Can you tell me why using the same window name in the windoe.open function call does not prevent multiple windows from opening?  
The only idea that I have about that is that for any reasons the window name got changed.
It is so that the window name is the name of an iframe or a frame in a frameset or a window the was opened by window.open() with second parameter being window name or a window that has set his own name by script assignment.
Like this:
<script>
window.name="myNewName";
</script>

But the window.open() method for sure reuses a window, iframe or frame if the name is found in any window on the desktop from same browser brand.

Avatar of paulgn

ASKER

OK, picking up on your idea about the window name getting changed, I just added two lines of code to my javascript function to show me the name:
<script type="text/javascript">
function open_ContextSensitiveHelpWindow(tagName){
  helpWin = window.open("help/help.html?context=help&topic="+tagName+"&","helpwin","resizable=1,width=700,height=350,scrollbars=1,left=275,top=275");
  var windowName = helpwin.name;
  alert("The window name is " + windowName):
  helpWin.focus();
}
</script>
The first time I clicked "Help", the help window opened and the alert printed a window name of "helpwin".   Leaving the help window on my desktop, I clicked the "Help" link again and a second help window opened and the alert printed a window name of "helpwin".  
Therefore, it "appears" the window name is the same for both windows.  Do you have any other ideas why the same window name is not preventing a second pop up?  Could it be the way I've structured my pageheader.jsp?  Or how I've embedded my javascript function with the jsp?  I've attached the jsp for your viewing.  Or is it possible the "help" function that I'm calling (it's a package called WebWorks) could change the name of the window internally?
Any ideas you have would be most appreciated.  Thanks.
Avatar of paulgn

ASKER

Hello ZVonko -
   Did you receive the question in my previous email?
  Thank you.

 
ASKER CERTIFIED SOLUTION
Avatar of paulgn
paulgn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial