Solved

i want pop up to show only once on page load

Posted on 2003-11-16
13
1,325 Views
Last Modified: 2010-04-09
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
0
Comment
Question by:ewok33
  • 4
  • 2
  • 2
  • +3
13 Comments
 
LVL 11

Expert Comment

by:Zontar
ID: 9761368
Need to know what's available on your server, please post a complete URL.
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 9761531
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.
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9761577
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
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9761581
first line in the PHP should be

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

sorry
0
 

Author Comment

by:ewok33
ID: 9761865
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
monvelasquez earned 160 total points
ID: 9761867
the solutions already provided are all server-side methods... here's one the you can use on the client-side using javascript

a good sample on how to set cookies in javascipt is at
    http://www.netspade.com/articles/javascript/cookies.xml
you can download cookies.js from there.

so instead of calling NewWindow when thw page loads try this..

--[CODE]---------------------------------------------------------------
<html>
</head>
<script type="text/javascript" src="cookies.js"></script>
<script language="javascript">
 function NewWindow(...) {
    // new window code here
 }

 function body_load() {
   var sawpop = getCookie('sawpop');
   if (sawpop != '1')  {
       NewWindow('http://www.sdfaf/subscribe.html','join','295','520','custom','front');
   }
   else {
       setCookie ('sawpop',1)
   }
 }
</script>
</head>
<body onload="body_load()">
...
</body>
</html>

--[/CODE]--------------------------------------------------------------
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9761904
Both PHP and ASP support include files.
0
 
LVL 3

Expert Comment

by:sicknote
ID: 9762985
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.
0
 
LVL 3

Expert Comment

by:monvelasquez
ID: 9762997
sicknote's right... my mistake.
0
 
LVL 10

Expert Comment

by:Bustarooms
ID: 9763651
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;
}
0
 
LVL 3

Expert Comment

by:sicknote
ID: 9763684
that's because the two functions and where to get them were mentioned in the first post that requires their use.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now