473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Maintaining state between function calls

I would like to add dynamic text sizing to a website. There will be
three text sizes. When the default is used, only the "Increase text
size" button is shown. When the maximum size is used, only the
"Decrease text size" button is shown. And otherwise, both buttons are
shown.

I've been working on a mockup as follows at the end, but my issue is
that once I have manipulated an element of the HTML DOM, if I access it
again to check it's value, I will only get the original value in the
document. To do the dynamic buttons, I need to maintain some kind of
state between function calls, so that I know what the current size is.
Can I even do this with Javascript or am I barking up the wrong tree?

-Melissa
-------------------------------------------------------------------
<html>
<head>
<link id="StylesheetReference" rel="stylesheet" type="text/css"
href="size1.css" />
<script type="text/javascript" language="JavaScript">
<!--
function useSize1() {
document.getElementById('StylesheetReference').hre f="size1.css"
}
function useSize2() {
document.getElementById('StylesheetReference').hre f="size2.css"
}
function useSize3() {
document.getElementById('StylesheetReference').hre f="size3.css"
}
//-->
</script>
</head>
<body>
<center>
<h1>bob-0-matic</h1>
<p>This is a mock-up of using Javascript to dynamically change the
font-size :)</p>

<script type="text/javascript" language="JavaScript">
<!--
if (document.getElementById('StylesheetReference').hr ef = "size1.css")
{
document.write("<p><a href=\"#\" onMouseDown=\"useSize2()\">Increase
size</a></p>")
}
else if (document.getElementById('StylesheetReference').hr ef =
"size2.css") {
document.write("<p><a href=\"#\" onMouseDown=\"useSize3()\">Increase
size</a></p>")
document.write("<p><a href=\"#\" onMouseDown=\"useSize1()\">Decrease
size</a></p>")
}
else if (document.getElementById('StylesheetReference').hr ef =
"size3.css") {
document.write("<p><a href=\"#\" onMouseDown=\"useSize2()\">Decrease
size</a></p>")
}
//-->
</script>
</center>
</body>
</html>

Jul 23 '05 #1
5 1512
I guess I should add that I would prefer to do it without cookies as
well... Otherwise problem solved :)

Jul 23 '05 #2
<me*************@gmail.com> wrote in message news:11*********************@f14g2000cwb.googlegro ups.com...
I would like to add dynamic text sizing to a website. There will be
three text sizes. When the default is used, only the "Increase text
size" button is shown. When the maximum size is used, only the
"Decrease text size" button is shown. And otherwise, both buttons are
shown.

I've been working on a mockup as follows at the end, but my issue is
that once I have manipulated an element of the HTML DOM, if I access it
again to check it's value, I will only get the original value in the
document. To do the dynamic buttons, I need to maintain some kind of
state between function calls, so that I know what the current size is.
Can I even do this with Javascript or am I barking up the wrong tree?

-Melissa
-------------------------------------------------------------------
<html>
<head>
<link id="StylesheetReference" rel="stylesheet" type="text/css"
href="size1.css" />
<script type="text/javascript" language="JavaScript">
<!--
function useSize1() {
document.getElementById('StylesheetReference').hre f="size1.css"
}
function useSize2() {
document.getElementById('StylesheetReference').hre f="size2.css"
}
function useSize3() {
document.getElementById('StylesheetReference').hre f="size3.css"
}
file://-->
</script>
</head>
<body>
<center>
<h1>bob-0-matic</h1>
<p>This is a mock-up of using Javascript to dynamically change the
font-size :)</p>

<script type="text/javascript" language="JavaScript">
<!--
if (document.getElementById('StylesheetReference').hr ef = "size1.css")
{
document.write("<p><a href=\"#\" onMouseDown=\"useSize2()\">Increase
size</a></p>")
}
else if (document.getElementById('StylesheetReference').hr ef =
"size2.css") {
document.write("<p><a href=\"#\" onMouseDown=\"useSize3()\">Increase
size</a></p>")
document.write("<p><a href=\"#\" onMouseDown=\"useSize1()\">Decrease
size</a></p>")
}
else if (document.getElementById('StylesheetReference').hr ef =
"size3.css") {
document.write("<p><a href=\"#\" onMouseDown=\"useSize2()\">Decrease
size</a></p>")
}
file://-->
</script>
</center>
</body>
</html>

You're using the assignment operator '=' when you need the equality operator '=='
if (document.getElementById('StylesheetReference').hr ef = "size1.css")


That test will always evaluate true regardless of the actual value.

--
Stephen Chalmers
Jul 23 '05 #3
Ah yes, sorry. I *did* fix that, but it still does not work, the link
href value still seems to be returning the default of "size1.css".
Anyhoo, I thought of a different solution, that changes the querystring
parameter to track the text size:
------------------------------*------------------------------*-------
<html>
<head>
<link id="StylesheetReference" rel="stylesheet" type="text/css"
href="size1.css" />

<script type="text/javascript" language="JavaScript">
<!--
function useSize1ByUrl() {
location.search = "ts=1"
}
function useSize2ByUrl() {
location.search = "ts=2"
}
function useSize3ByUrl() {
location.search = "ts=3"
}
function useSize4ByUrl() {
location.search = "ts=4"
}
//-->
</script></head>
<body><center>
<p>This is a mock-up of using Javascript to dynamically change the
font-size :)
<br /> ...As well as dynamically choose which links to show</p>
<table border="1" bgcolor="lightblue">
<script type="text/javascript" language="JavaScript">
<!--
if (location.search == "" || location.search == "?ts=1") {
document.getElementById('StylesheetReference').hre f="size1.css"
document.write("<tr><td onClick=\"useSize2ByUrl()\"><a
href=\"#\">Increase size</a></td></tr>")
}
else if (location.search == "?ts=2") {
document.getElementById('StylesheetReference').hre f="size2.css"
document.write("<tr><td onMouseDown=\"useSize3ByUrl()\">Increase
size</td></tr>")
document.write("<tr><td onMouseDown=\"useSize1ByUrl()\">Decrease
size</td></tr>")
}
else if (location.search == "?ts=3") {
document.getElementById('StylesheetReference').hre f="size3.css"
document.write("<tr><td onMouseDown=\"useSize4ByUrl()\">Increase
size</td></tr>")
document.write("<tr><td onMouseDown=\"useSize2ByUrl()\">Decrease
size</td></tr>")
}
else if (location.search == "?ts=4") {
document.getElementById('StylesheetReference').hre f="size4.css"
document.write("<tr><td onMouseDown=\"useSize3ByUrl()\">Decrease
size</td></tr>")
}
//-->
</script>
</table></center></body></html>

Jul 23 '05 #4


<me*************@gmail.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com...
Ah yes, sorry. I *did* fix that, but it still does not work, the link
href value still seems to be returning the default of "size1.css".

Yes it would, because that is the value coded in the html and at the point you'r reading it, no attempt has
been made to change it.
Anyhoo, I thought of a different solution, that changes the querystring
parameter to track the text size:

<SNIP>

There's no need to have four blocks of code to handle the different values of the search parameter, or to have
four functions to reload the page with the appropriate parameter. In fact, there's no need to reload the page
at all.

Try this:

<html>
<head>
<link id="StylesheetReference" rel="stylesheet" type="text/css"
href="size1.css" />

<script type="text/javascript" language="JavaScript">

var maxSize=4, fSize=1;

for(var i=0; i<maxSize && location.search!="?ts="+(i+1); i++) /*read parameter*/
;

fSize=(i==maxSize) ? 1 : i+1; /*if not found, use 1*/

function useSize( theSize )
{
if( theSize <= maxSize && theSize >= 1 ) /*ensure selected size within range*/
{
var ssRef=null;

fSize=theSize;

/*Set stylesheet and select visibility of links*/

if( document.getElementById && (ssRef=document.getElementById('StylesheetReferenc e')) )
{ ssRef.href="size"+theSize+".css";
document.getElementById('incBtn').style.visibility =(fSize==maxSize?'hidden':'visible');
document.getElementById('decBtn').style.visibility =(fSize==1?'hidden':'visible');
}
}

}

</script>
</head>
<body>
<center>

<!-- Split-infinitive deleted -->

<p>This is a mock-up of using Javascript to change the font-size dynamically :) <br />
....As well as choose dynamically which links to show</p>
<table border="1" bgcolor="lightblue">

<script type="text/javascript">
if(document.getElementById) /*Display links */
{
document.write("<tr id='decBtn'><td onClick='useSize(fSize-1)'><a href='#'>Decrease size</a></td></tr>")
document.write("<tr id='incBtn'><td onClick='useSize(fSize+1)'><a href='#'>Increase size</a></td></tr>")
useSize(fSize); /*set initial stylesheet, which initilises visibility of buttons*/
}
</script>
</table>
</center>
</body>
</html>
--
Stephen Chalmers http://makeashorterlink.com/?H3E82245A

547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134

Jul 23 '05 #5
<me*************@gmail.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com...
Ah yes, sorry. I *did* fix that, but it still does not work, the link
href value still seems to be returning the default of "size1.css".
It would, because that is the value coded into the html and at the point you're reading it, no attempt has
been made to change it.

Anyhoo, I thought of a different solution, that changes the querystring
parameter to track the text size:


<SNIP>

There's no need to have four blocks of code to handle the different values of the search parameter, or to have
four functions to reload the page with the appropriate parameter. In fact, there's no need to reload the page
at all.

Try this:

<html>
<head>
<link id="StylesheetReference" rel="stylesheet" type="text/css"
href="size1.css" />

<script type="text/javascript" language="JavaScript">

var maxSize=4, fSize=1;

for(var i=0; i<maxSize && location.search!="?ts="+(i+1); i++) /*read parameter*/
;

fSize=(i==maxSize) ? 1 : i+1; /*if not found, use 1*/

function useSize( theSize )
{
if( theSize <= maxSize && theSize >= 1 ) /*ensure selected size within range*/
{
var ssRef=null;

fSize=theSize;

/*Set stylesheet and select visibility of links*/

if( document.getElementById && (ssRef=document.getElementById('StylesheetReferenc e')) )
{ ssRef.href="size"+theSize+".css";
document.getElementById('incBtn').style.visibility =(fSize==maxSize?'hidden':'visible');
document.getElementById('decBtn').style.visibility =(fSize==1?'hidden':'visible');
}
}
}

</script>
</head>
<body>
<center>

<!-- Split-infinitive deleted -->

<p>This is a mock-up of using Javascript to change the font-size dynamically :) <br />
....As well as choose dynamically which links to show</p>
<table border="1" bgcolor="lightblue">

<script type="text/javascript">
if(document.getElementById) /*Display links */
{
document.write("<tr id='decBtn'><td onClick='useSize(fSize-1)'><a href='#'>Decrease size</a></td></tr>")
document.write("<tr id='incBtn'><td onClick='useSize(fSize+1)'><a href='#'>Increase size</a></td></tr>")
useSize(fSize); /*Set initial stylesheet, which initilises appropriate visibility of buttons*/
}
</script>
</table>
</center>
</body>
</html>
--
Stephen Chalmers http://makeashorterlink.com/?H3E82245A

547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134


Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: leegold2 | last post by:
Probably a newbie question about "state": My problem is I have a search form, so user enters a keyword <enter>, then this form posts to another page were the result are displayed. But this...
0
by: Dinesh Upare | last post by:
Hi, I want to fire events of Datatgrid without maintaining the view state. If I disable the ViewState then the events are not fired. The reason I don't want to maintain view state is because its...
1
by: Novice | last post by:
Hey all, I have finally managed to create a Custom WebControl and am using a technique from another programmer to maintain state between pages - I would just like to validate this idea. ...
4
by: dmitri.pekker | last post by:
I'm relatively new to web services and I stumbled on some code that maintains state between calls using a class with static members. This is not the actual code, but you get the gist: void...
1
by: Deepson Thomas | last post by:
Hi, Currently iam facing a strange problem.. One dropdown in my page is keeping itz state after postback. Whichever the item i selected is not the selected item after postback. it goes back to the...
5
by: Deepson Thomas | last post by:
Hi, Currently iam facing a strange problem.. One dropdown in my page is not keeping itz state after postback. Whichever the item i select irrespective of that after the post-back the default...
1
by: clintonG | last post by:
I'm having a problem maintaining state with a Panel control in a MasterPage and I need help thinking through this process. The basic structure of the HTML in the Master looks like this... ...
0
by: mark.norgate | last post by:
Hi I'm having a problem in adding controls to a page programmatically in response to a button click. Composite user controls added programmatically in the CreateChildControls() method work...
4
by: Sam | last post by:
I have an asp.net 2.0 app that uses a sitemap, Master Page, and has several content pages. While this feature has simplified the process of creating a data-driven site menu, it does seem to have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.