[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ajug-members]: javascript window.open single window



I'm not sure I completely follow the JavaScript you've got there.  Maybe a
little different approach can do what you want.  Are you just interested in
having a JavaScript routine that runs when your page is loaded that
opens/refocuses a single other window and displays a given URL in that
window?  That's what it sounded like from your description.  If that's the
case, I'd just use something like the code in this example (HTML file
inlined in case the listserv blocks attachments, you should save it as
tester.html somewhere that your web server can serve it)

Jeff

--- snip ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
 <title>Untitled</title>
 <script language="JavaScript">
 <!-- 
 /* open the requested URL in a single window that replaces whatever
  may be in there from last time.
  The key is that giving the window the same name every time will
  result in the browser re-using it.
 */
 function doPopup(URL) {
  var popupWindow = window.open(URL, "SingleWindowThatGetsReused");
  // in case it's minimized or hidden behind other stuff
  popupWindow.focus();
 }

 /* determine whether we need to popup the window, basically
  whether or not the form was posted, or whether this is the
  initial load of the page
 */
 function popupIfNeeded() {
  var targetSite = unescape(getUrlVar("siteURL"));

  if(targetSite != "")
   doPopup(targetSite);
 }

 /* helper function, retreives the value of a given URL var
 */
 function getUrlVar(varName) {
  var allVars = window.location.search;
  var NOT_FOUND = -1;

  var searchTokenStart = varName + "=";
  var searchTokenEnd = "&";

  var indexStart = allVars.indexOf(searchTokenStart);

  // requested URL var not present in URL string
  if(indexStart == NOT_FOUND)
   return "";

  // need to remove the URL var name
  indexStart = indexStart + searchTokenStart.length;

  var indexEnd = allVars.indexOf(searchTokenEnd, indexStart);

  // requested URL var is last one in URL string, no "&" before
  // next one, therefore take all remaining chars in URL string
  if(indexEnd == NOT_FOUND)
   indexEnd = allVars.length;

  var varValue = allVars.substring(indexStart, indexEnd);

  return varValue;
 }

 /* validate the form, make sure they entered a URL
 */
 function validate(theForm) {
  if(theForm.siteURL.value != "")
   return true;
  else {
   alert('Please enter a site');
   theForm.siteURL.focus();
   return false;
  }
 }
 //-->
 </script>
</head>

<body onload="popupIfNeeded()">

<form action="tester.htm" method="get" onsubmit="return validate(this)">
 Enter site to go to:
 <input type="Text" name="siteURL" value="">
 <input type="Submit" value="Go">
</form>

</body>
</html>
--- snip ---

----- Original Message ----- 
From: "Mani Gudavalli" <Manidhar.Gudavalli@eds.com>
To: <ajug-members@ajug.org>
Sent: Thursday, March 11, 2004 5:04 PM
Subject: [ajug-members]: javascript window.open single window


> HI
> I have this javascript. When I poast my page to itselft based on the
> data this sctipt is executed and loads a popup window. However, each
> time I submit the page it is opening a new window. How to control my
> javascript to allways focus on the same popup window.
> In popup window , I display one page but redirected to a different site
> in the popup.
>
> example: popup link: mylink is a.jsp but that is redirected to
www.yahoo.com
>
> thanks
> MANI
>
> function popupnr(mylink, windowname, refocus, wrongData)
> {
>
> var mywin, href;
> if (typeof(mylink) == 'string')
> href=mylink;
> else
> href=mylink.href;
> if (!window.mywin ) {
> mywin = window.open('', windowname,
'width=800,height=600,scrollbars=yes');
> }
> // if we just opened the window
> if (
> mywin.closed ||
> (! mywin.document.URL) ||
> (mywin.document.URL.indexOf("about") == 0)
> )
> mywin.location=href;
> else if (refocus)
> mywin.focus();
> return false;
>
> }
> //-->
> </SCRIPT>
>
>
>