Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing and retrieving screen size in cookies 1

Status
Not open for further replies.

GubaTech

Programmer
Nov 3, 2005
9
0
0
IT
Dear All,

i have the problem to store the window size somewhere and - once the user returns - restore his window size.
This torage shoud have been done when an OnResize event is thrown.
I know that this is quite easy to do in javascript, but I don't know such language.
Is there anybody who can help me ?
Thanks in advance

Sergio
 
this should give you a good start, it's untested:
Code:
<script type="text/javascript">

function setScreenSizeCookie() {
   var x = document.body.offsetWidth;
   var y = document.body.offsetHeight;
   setCookie("screenWidth", x, new Date("1/1/2099"));
   setCookie("screenHeight", y, new Date("1/1/2099"));
   alert("cookies set");
}

function restoreScreenSize() {
   var x = getCookie("screenWidth");
   var y = getCookie("screenHeight");
   alert("test");
   window.resizeTo(x, y);
}

function getCookie(name) {
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure) {
   document.cookie = name + "=" +escape(value) +
   ((expires) ? ";expires=" + expires.toGMTString() : "") +
   ((path) ? ";path=" + path : "") + 
   ((domain) ? ";domain=" + domain : "") +
   ((secure) ? ";secure" : "");
}

window.onresize = restoreScreenSize;

</script>

<body>
push the button before you resize the window<br /><br />
<input type="button" value="set cookies" onclick="setScreenSizeCookie()" />
</body>

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Thak you very much.
I will try it.
The fact is that this script does prevent the user to resize the window, but it should only store the new window size in the cookie so that the log-in page would restore the last window size.
May you help me a little more ?
In the normal pages a part of the script will store the window size in the cookie. BTW, where do I change the cookie name in Javascript ?
However, in the login page the other part of thescript will read the last windo size and restore them as the user open the log-in page.

Thanks in advance

Sergio
 
Well.... I think your logic is a bit flawed....

You want at the login page to reset the size of the window to the values set in the cookie, but the cookie is not set until after they have logged in and the cookie is set at a different page?

I would think that the cookie would need to be set before they hit the login page, or at least have the cookie set at the login page where it is being accessed.

Nevertheless, if you want the cookies to be stored on some page in your application, then invoke the setCookie function on that page. Then use the getCookie function in the login page to retrieve those values. Provided that you have set an expire date some time in the distant future, the cookie will still exist when you navigate back to the login page.

BTW, where do I change the cookie name in Javascript ?

That is set when invoking the setCookie function. The first string parameter passed to that function is the name of the cookie (which is why that parameter is called "name")

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
I DID IT with your help!
This is how I did:
On the normal pages:
Code:
<script language="JavaScript">
<!--

function setCookie(name,value,expires,path,domain,secure) {
   document.cookie = name + "=" +escape(value) +
   ((expires) ? ";expires=" + expires.toGMTString() : "") +
   ((path) ? ";path=" + path : "") +
   ((domain) ? ";domain=" + domain : "") +
   ((secure) ? ";secure" : "");
}

function setScreenSizeCookie() {
   var x = document.body.offsetWidth;
   var y = document.body.offsetHeight;
   setCookie("cookieWIDTH", x, new Date("1/1/2099"));
   setCookie("cookieHEIGHT", y, new Date("1/1/2099"));
}

window.onresize = setScreenSizeCookie;
//-->
</script>

while on the log-in page:

Code:
function getCookie(name) {
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}

function restoreScreenSize() {
   var x = getCookie("AXAMIVERWINWIDTH");
   var y = getCookie("AXAMIVERWINHEIGHT");
   window.resizeTo(x, y);
}
with a OnLoad="restoreScreenSize();" into the BODY declaration.

Thanks again for your MOST valuable help.

Sergio
 
P.S.

it is true: the cookie must exist, at least if I want that the log in page is formatted at the right size.
Well, the fact is that the log-in page is opened in a new window by a launcher which creates a pop-up without url, navigation button etc.
So it is also opened at a standard size.
Whis is why I check the existence of the cookie before, and then if the cookies doesn't exist the OnLoad behaviour won't be inserted into the BODY declaration.

Thanks again.

Sergio
 
Glad you got it working [thumbsup2]

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top