Click to See Complete Forum and Search --> : Funtion to control a window


_LOBO_
04-17-2003, 08:55 AM
Hi TNX for any help.

I try to open a window in a onload event using a funtion but Im not sure if this script is right, this last one can be optimazed ?

thank you.


<HEAD>
<TITLE></TITLE>


<SCRIPT LANGUAGE="javascript">


function NewWindow(mypage, myname,w ,h , scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open('http://www.cnn.com', myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

</script>

</HEAD>
<BODY onLoad="NewWindow( this.href,'name','400','400','no');return false;">
</BODY>
</HTML>

havik
04-17-2003, 09:15 AM
what exactly do you want to have happen here? Because it looks fine to me.

Havik

_LOBO_
04-17-2003, 09:24 AM
I want to launch a popup using a cookie. The funtion for the cookie works fine but the popup is in the corner and I should looks better if the popup is in the center.

this the original funtion for center the popup:_______________



<html>
<head><title>POP</title>
<script language="javascript" type="text/javascript">
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? parseInt((screen.width-w)/2) : 0;
TopPosition = (screen.height) ? parseInt((screen.height-h)/2) : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
if(win.window.focus){win.window.focus();}
}
</script>
</head>
<body>
<a href="javascript:NewWindow('new.html','name',400,300,'no');">Open Centered New Window</a>
</body>
</html>


Here is the cookie and how the cookie control the popup__________________________________________________

<script LANGUAGE="JavaScript">

<!-- Popup script on exit supplied by Working-At-Home-Business.com. -->

<!-- You may need to edit 3 places in the script:
<!--You can set the number of days the cookie should last by editing "var expDays=number of days". -->
<!--You may also need to change the width, height and other attributes by editing "var windowprops=xxxx". -->
<!--You need to assign the page to pop up by editing "var page=yourpage". -->


<!-- Begin
var expDays = 5; // number of days the cookie should last

var page = "popup.htm";
var windowprops = "width=280,height=320,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no";

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);
}
}
// End -->
</script>


______________________________________________________


I want put all togheter, I was trying to put in the popup a onload function to center this window but dosent work, this last one creats an other popup. Is all a mess.
Can you help me to make this work? just control the popup and put it in the center of the web browser

Thanks in advance.

havik
04-17-2003, 09:31 AM
Well, your first problem is this line:
<a href="java script:NewWindow('new.html','name',400,300,'no');">Open Centered New Window</a>

There should be no space between 'java' and 'script'. So change it to this:
<a href="Javascript:NewWindow('new.html','name',400,300,'no');">Open Centered New Window</a>

I tested it with the NewWindow function from your second post and it worked (it centered it nicely).

I haven't looked at the cookie section yet but I will shortly.

Havik

_LOBO_
04-17-2003, 09:36 AM
TNX Havik.

We can launch the popup from the cookie and at the same time (using this same script) put the popup in the center?:confused:

havik
04-17-2003, 09:49 AM
You could but you'd need to retrieve the values from the cookie in the NewWindow function

var win = null;
function NewWindow(mypage,myname,w,h,scroll){

// Keep the LeftPostion and Top Position as it is
// Just insert them below or above, wherever

settings = 'top='+TopPosition+',left='+LeftPosition+'

// then retrieve the other values from the cookie, I'm not
// sure which function you use (getCookieVal?)

settings += getCookieVal(); // or whichever function you use

win = window.open(mypage,myname,settings)
if(win.window.focus){win.window.focus();}
}

Something along those lines. I've only worked with cookies once so I'm not too sure on this. Just make sure that your storing the settings into the cookie and retrieving them only.

If you want to add the page name, then make sure that your retrieving the page name and settings from the cookie separately.

Havik

_LOBO_
04-17-2003, 10:03 AM
I dont know how to star I'm a newbie and the cookies is something new for me as you can see I grab the cookie function from a web site call it Working-At-Home-Business.com in this last one you can find free javascript code but I dont really understad how it works. Can you help me with this ? I never see somethig like this before coz you can control the how much time the people will see the popup and this dont botter as other popups.

U think that the most easy solution is use onload event and when the popup is loading take the right position for him self?

havik
04-17-2003, 10:34 AM
Well, what exactly do you want to use the cookies for? The window pops up and centers without any use of cookies at all. The only reason I could think of for you to use cookies is to either:
control it so that it'll only pop up once (like on the first visit so that it won't keep popping up whenever the window is refreshed)
Or, to allow the user to change which page loads up in the pop up window.

It'll help if I knew why the cookies are necessary, which is why I can't answer that last question yet.

Havik

_LOBO_
04-17-2003, 10:53 AM
ok The popup needs to be launch using a link in the original code, I modified this is script to be used in onload event but when we launch the web site this last one launch a popup in the corner and this last one launch other popup in the middle of the browser so there is a error.

M y idea for use the cookie is to control the aparition of the popup so this last one pops once but I ask my self if using this script (cookie script) we can not only launch a popup but also put it in the center of the browser. If you use the cookie script you can see that this one launch a popup in the corner and not in the center. Can we change that?.:confused:

Thanks for your quick responce :)

havik
04-17-2003, 12:22 PM
I'll still confused :confused: How about you post a link to the page that's giving you problems so I can see what you're talking about.

Here's the way I see it:

You want to open a pop up window when the page loads. This window has to be centered. This is the only window that is being popped up. You want to use cookies to make sure that this window only pops up once while on this page. Meaning that if reload is hit or the user leaves the page and comes back then it still won't pop up. The only way the window will pop up again is if the user completely leaves your site (including other pages of your own).

Does all that sound right?

Havik

_LOBO_
04-18-2003, 09:58 AM
The only way the window will pop up again is if the user come in 5 days more or less that depends of the cookie, the cookie make me able to do that.

and all the others things you write are right, That is hte idea. The main problem is that I cant center the popup when this is launch from the cookie.

TNX for be patient with me :D

DrDaMour
04-18-2003, 12:23 PM
your problem is simple, your cookie fucntion is calling window.open() which will NOT center stuff, the function to center the window is

your function NewWindow(mypage, myname,w ,h , scroll)



function checkCount() {
var count = GetCookie('count');
if (count == null) {
count=1;
SetCookie('count', count, exp);

window.open(page, "", windowprops);

}


i see what you were trying to do, but this is a scope problem, see since you build windowprops in the newWindow function it can only be used in that fucntion. Once you go to anohter function it no longer is valid.

so change that window.open to:
your function NewWindow(page, "",w ,h , scroll)
you of course have to choose the last three parameters

_LOBO_
05-02-2003, 06:43 AM
Thank you Havik and DrDaMour, you guys help a LOT VOILA! the code this last one works fine.

:D

eNjO iT


<script LANGUAGE="JavaScript">
<!-- You may need to edit 3 places in the script:
<!--You can set the number of days the cookie should last by editing "var expDays=number of days". -->
<!--You may also need to change the width, height and other attributes by editing "var windowprops=xxxx". -->
<!--You need to assign the page to pop up by editing "var page=yourpage". -->

<!-- Begin
var expDays = 5; // number of days the cookie should last
<!--codigo original: var page = "popup.htm";
<!--codigo original: a var windowprops = "width=280,height=320,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no";

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

<!--codigo original: window.open(page, "", windowprops);

<!--codigo nuevo:
var popupwidth=580 <!-- MEDIDA DEL POPUP
var popupheight=210 <!-- MEDIDA DEL POPUP
var scrwidth=screen.availwidth
var scrheight=screen.availheight
var popupleft=(scrwidth-popupwidth)/2
var popuptop=(scrheight-popupheight)/2
window.open('popup.htm','name','height='+popupheight+',width='+popupwidth+',left='+popupleft+',top=' +popuptop)
<!--codigo nuevo END