Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

i want pop up to show only once on page load

Posted on 2003-11-16
13
1,515 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

861 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