Forum Moderators: open

Message Too Old, No Replies

need help with if-else statements for a button

         

someone

6:21 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



is this possible to do?
I have a button. I want to do something that if the user clicks on it the first time, it takes the user to a webpage. Then when the user clicks on the button for the 2nd time, the button will be able to detect that it has already been clicked once, and it will do the "else" part.

rocknbil

6:30 pm on Jan 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Javascript works within the context of the current page, so there's nothing inherent that will "remember" the value from the last page unless you pass a string to it and parse it with Javascript.

However, you could set a document cookie with Javascript, and the next page **could** read that value and react accordingly.

var clicked=false;
onclick=increment();

function increment() {
clicked=true;
(...set cookie code ...)
}

Then on page 2,

clicked = (... read cookie ...)
if (clicked==false) { action; }
else { other action ; }

birdbrain

6:53 pm on Jan 20, 2005 (gmt 0)



Hi there someone,

it is possible if you use window.open like this...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>if...else</title>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<script type="text/javascript">
//<![CDATA[

var check="true";
function doThisOrThat() {
if(check=="true") {
window.open("http://www.webmasterworld.com/","","left=200,top=200,width=400,height=400");
check="false";
}
else {
alert("this button has already been clicked")
}
}

//]]>
</script>

</head>
<body>

<form action="#">
<div>
<input type="button" value="the button" onclick="doThisOrThat()"/>
</div>
</form>

</body>
</html>

birdbrain

someone

11:14 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



Thanks Rocknbil and Birdbrain.

I was wondering, is there a way to set the new window height and width in percentage, like 100%, instead of giving it a fixed pixel size?

rocknbil

4:45 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not directly, but if you are using Javascript you should be able to calculate it. Something like

<html>
<head>
<title>newwin</title>

<script language="JavaScript">
function newWin() {
var ww,wh,params,win;
ww = (screen.width)?parseInt(screen.width*.75):400; // always a default
wh = (screen.height)?parseInt(screen.height*.5):300;
params = 'width='+ww+',height='+wh;
win = open('','tstwin',params);
}
</script>

</head>
<body>
<a href="#" onClick="newWin();">new window</a>
</body>
</html>

You could do a similar thing to set the window position.

someone

11:10 pm on Jan 24, 2005 (gmt 0)

10+ Year Member



how do I go about doing it on a per session basis? so if the user clicks on the button the first time, it does the "if" part then when the user clicks on it the second, third, fourth....time, it does the "else" part. So the "if" action is only done once until the user closes the browser. Thanks.