Link to home
Start Free TrialLog in
Avatar of ewok33
ewok33

asked on

i want pop up to show only once on page load

my main index page index.shtml has a onload command to pop up a small window

<body onLoad="NewWindow('http://www.sdfaf/subscribe.html','join','295','520','custom','front');"

problem is that because this is the 'home' page and my nav bar points back to this index page with the pop up, when a user has drilled down into other pages then wants to return to the home page, the pop up is going to keep poping up. i have seen on other sites an option for the pop up to be seen only once, it must of been a link, but it gives the user a choice. not bothered about that. i just want it to pop up only once. what code would i use for this.

thanks in advance.

pls provide exactly where the code should go. cheers

fk
Avatar of Zontar
Zontar

Need to know what's available on your server, please post a complete URL.
If your server allows you to use Server Side code (ASP, JSP etc), you could use a cookie to record whether the pop up window has already appeared.
The process (in ASP anyway) would be something like this for the page.  It replaces the code you currently use for your BODY tag as written in your question...

-------------------------------------------------------------------------------------
<% ' check for popup cookie and launch popup if it doesn't exist
If Request.Cookies ("POPUPCHECKER")("Displayed") <> "Yes" Then

  ' set cookie so this result doesn't occur again
  Response.Cookies ("POPUPCHECKER")("Displayed") = "Yes"
  ' load page with popup code %>
  <body onLoad="NewWindow('http://www.sdfaf/subscribe.html','join','295','520','custom','front');">

<% ELSE
  ' cookie was set when popup appeared last time, so produce different result this time %>
  <body>

<% END IF %>
-------------------------------------------------------------------------------------

Its been a while since I last did this, but it should work okay, just don't forget to set your page to an .asp extension.
PHP version:

<?php
  if( !isset($_COOKIE["sawpopup"]) && $_COOKIE["sawpopup"] == "true")
  {
    setcookie("sawpopup", "true");
?>
<script type="text/javascript">
//  definition for NewWindow() function goes here...

window.onload = function(){ NewWindow('http://www.sdfaf/subscribe.html','join','295','520','custom','front'); };
</script>
<?php
  }
?>

ASP version:

<%
If Request.Cookies("sawpopup")("Displayed") <> "Yes" Then
  Response.Cookies("sawpopup")("Displayed") = "Yes"
%>
<script type="text/javascript">
//  definition for NewWindow() function goes here...

window.onload = function(){ NewWindow('http://www.sdfaf/subscribe.html','join','295','520','custom','front'); };
</script>
<%
End If
%>

No need to include the function if it's not going to be called, and using the window.onload handler in script is cleaner than trying to echo part of a body tag or echoing two different body tags IMO.

Cookie not set -->> script section containing both the function definition and the call to the function written to the page; set the "sawpopup" cookie

Cookie set -->> script section not written to page, not needed
first line in the PHP should be

 if( !( isset($_COOKIE["sawpopup"]) && $_COOKIE["sawpopup"] == "true" ) )

sorry
Avatar of ewok33

ASKER

unless im wrong, i cant change my index.shtml to asp or php cos im calling a ssi page within it, so it has to be .shtml. i could be wrong on this and if i am , pls let me know.

server details are:

asp
php


let me know if it will work or what will work if i have to keep the .shtml extension.

cheers

fk
ASKER CERTIFIED SOLUTION
Avatar of monvelasquez
monvelasquez

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
Both PHP and ASP support include files.
should the function body_load in the client side javascript solution be:

function body_load() {
   var sawpop = getCookie('sawpop');
   if (sawpop != '1')  {
       NewWindow('http://www.sdfaf/subscribe.html','join','295','520','custom','front');
       setCookie ('sawpop',1)
   }
 }

with the setCookie ('sawpop',1) in the else statement, it can only be called if sawpop is equal to 1.

that code can never be reached, and therefore the popup will always open.
sicknote's right... my mistake.
neither of you guys posted the functions for getCookie() and setCookie()


function getCookie (name)
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen)
  {
           var j = i + alen;
      if (document.cookie.substring(i, j) == arg){
         return getCookieVal (j);
      }

      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
      
  }
  return null;
}

function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
that's because the two functions and where to get them were mentioned in the first post that requires their use.