Page 1 of 1

Popup window - Javascript

Posted: Tue Jun 19, 2007 9:05 pm
by webdev
Hi guys,

Code: Select all

var expDays = 1; // number of days the cookie should last

var page = "popwin.html";
var windowprops = "width=300,height=300,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes";

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 SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function amt(){
  var count = GetCookie('count')
  if(count == null) {
    SetCookie('count','1')
    return 1
  } else {
    var newcount = parseInt(count) + 1;
    DeleteCookie('count')
    SetCookie('count',newcount,exp)
    return count
  }
}

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

function checkCount() {
  var count = GetCookie('count');
  if (count == null) {
    count=1;
    SetCookie('count', count, exp);
    window.open(page, "", windowprops);
  } else {
    count++;
    SetCookie('count', count, exp);
  }
}

window.onload=checkCount;

Code: Select all

<body onLoad="checkCount()">
How to when you have a popup window which opens on your homepage every time a visitor returns?

Any helps that would be great!

Posted: Tue Jun 19, 2007 9:08 pm
by superdezign
Eww. Are you serious?

I believe window.open or something. I'll check.

Edit: Yea, window.open().

Posted: Tue Jun 19, 2007 9:09 pm
by feyd
"onload" is typically all that's required, provided their browser doesn't block automatic pop-ups.

Your pop-up will open happen if "count" is found to be null, from GetCookie().

Posted: Wed Jun 20, 2007 12:47 am
by webdev
Well, at the moment, the pop up window doesn't open.

superdezign - do you mean window.open=checkCount; in javascript and what about html?

Feyd - yeah but doesn't work. Can you me give the code of this please?

Posted: Wed Jun 20, 2007 2:32 am
by Gente
superdezign - do you mean window.open=checkCount; in javascript and what about html?
Have tried to search 'window.open' in Google before ask this?
window.open is a function, not an event.
http://developer.mozilla.org/en/docs/DOM:window.open
http://msdn2.microsoft.com/en-us/library/ms536651.aspx
Please look at these pages. Hope these examples will help you.

Posted: Wed Jun 20, 2007 3:14 am
by onion2k
Pop-up windows launched from a non-interactive event (eg document loading) are always blocked by pop-up blockers. Some blockers even stop windows opened by interactive events like buttons.

Long story short: Pop ups aren't guaranteed to open regardless of how you launch them. Do not rely on it opening for all users because it just won't.

Posted: Wed Jun 20, 2007 3:25 am
by webdev
Thanks, Gente. Your examples talking about when the user click the button, the small window will be pop up.

I was looking for popup window that only displays the first time the visitor enters my site. But now it's not working!

Posted: Wed Jun 20, 2007 3:27 am
by webdev
I understand, onion2k but my client really want this. This is for promotion for short term.

Posted: Wed Jun 20, 2007 6:09 am
by superdezign
Popups never mean promotion. Popups mean annoyance.

Try creating a pseudo window on the actual website. Just a big absolutely positioned div that has an "X" in the corner that makes the div go away.

Posted: Wed Jun 20, 2007 7:48 pm
by webdev
superdezign, do you have a code for pseudo window? If yes, can you please give me a code? Also, show me the sample of this.

Thanks.

Posted: Wed Jun 20, 2007 7:57 pm
by superdezign
It's honestly no more than what I've described to you. An absolutely positioned div. It's all CSS and JavaScript, and the JavaScript does nothing other than display it and allow it to be hidden. It's a simple creation.

I'll write a little tutorial on making one, but in the meantime, you can probably do this yourself. You need position:absolute, possibly z-index, onLoad, and onClick. In it's simplest form, it's a quick addition.

Posted: Wed Jun 20, 2007 8:49 pm
by webdev
Ok then. I don't know what do you mean that but I will try to this as best as possible. Also I haven't heard pseudo window before.

In fact, I am sure the javascript scripting is work but the pop-up window doesn't disappear.

Posted: Wed Jun 20, 2007 9:22 pm
by superdezign
Pseudo basically just means "fake."

And you'd have a hidden div, and onload, you'd make it displayed, and when someone clicked the "X," it'd be hidden again.

Posted: Wed Jun 20, 2007 9:43 pm
by webdev
Oh really. Bit like first visitor pop-up window.

Well, I have to find the code for this because I haven't done this before.