Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

i want pop up to show only once on page load

Posted on 2003-11-16
13
Medium Priority
?
2,010 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
11 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
LVL 3

Accepted Solution

by:
monvelasquez earned 640 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

783 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