Solved

i want pop up to show only once on page load

Posted on 2003-11-16
13
1,238 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 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

12 Experts available now in Live!

Get 1:1 Help Now