mflam
asked on
Lock links after first click on a link?
How do I do the following:
I want to use javascript to lock the links of a page, once a link was clicked.
Since it's my page, I don't have to use real links (although if possible it's prefered).
Example:
Say there are two links on my page:
http://www.ibm.com
and http://www.microsoft.com
Now once the first link is clicked I want the others to become inactive.
ADVAthanksNCE, Moshe
PS. I'll give the question more points as they come...
I want to use javascript to lock the links of a page, once a link was clicked.
Since it's my page, I don't have to use real links (although if possible it's prefered).
Example:
Say there are two links on my page:
http://www.ibm.com
and http://www.microsoft.com
Now once the first link is clicked I want the others to become inactive.
ADVAthanksNCE, Moshe
PS. I'll give the question more points as they come...
Do you use frames?
ASKER
Here's what I figured out myself meanwhile:
========================== =====
Using onClick and return(false)
========================== =====
If I write:
<a href=http://www.ibm.com
onClick="if (locked) return(false) else { locked = true; return(true)}
IBM </a>
It works for all links!
But if I use:
<a href=http://www.ibm.com
onClick="linkOnce()"
IBM </a>
and in the javascript section after the var: locked = false I add:
linkOnce()
{
if (true == locked)
return(false)
else
{
locked = true;
return(true);
}
}
This for some reason does not work!
Can someone tell me how to fix it?
ADVAthanksNCE, Moshe
==========================
Using onClick and return(false)
==========================
If I write:
<a href=http://www.ibm.com
onClick="if (locked) return(false) else { locked = true; return(true)}
IBM </a>
It works for all links!
But if I use:
<a href=http://www.ibm.com
onClick="linkOnce()"
IBM </a>
and in the javascript section after the var: locked = false I add:
linkOnce()
{
if (true == locked)
return(false)
else
{
locked = true;
return(true);
}
}
This for some reason does not work!
Can someone tell me how to fix it?
ADVAthanksNCE, Moshe
You may be having a scope problem. I have had things like this happen before. "debugger" should tell you here.
I think I fixed it w/ an eval().
Another way to do it (w/ some browsers) is innerHTML (or outer or Text). Change it to <A id=IBM name=IBM href="">bla</A>.
I think I fixed it w/ an eval().
Another way to do it (w/ some browsers) is innerHTML (or outer or Text). Change it to <A id=IBM name=IBM href="">bla</A>.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Exactly your code may look like this (I forgot '>' after <a href=http://www.ibm.com
onClick="return linkOnce()" in previous comment) :
<html>
<head>
<title></title>
<script>
var locked = false;
function linkOnce()
{
if (locked)
return(false)
else
{
locked = true;
return(true);
}
}
</script>
</head>
<body>
<a href="javascript:alert('cl ick');"
onClick="return linkOnce()">IBM</a>
<a href="javascript:alert('cl ick');"
onClick="return linkOnce()">MBI</a>
</body></HTML>
onClick="return linkOnce()" in previous comment) :
<html>
<head>
<title></title>
<script>
var locked = false;
function linkOnce()
{
if (locked)
return(false)
else
{
locked = true;
return(true);
}
}
</script>
</head>
<body>
<a href="javascript:alert('cl
onClick="return linkOnce()">IBM</a>
<a href="javascript:alert('cl
onClick="return linkOnce()">MBI</a>
</body></HTML>
ASKER
And a friend of mine showed me how the code could be even more elegant using a return of "isFirstClick"! instead of "isLocked".
Thanks! Moshe
Thanks! Moshe
<a href="javascript:open_link
and your javascript will be something like:
<script language="javascript">
<!--
var locked = false
function open(new_url) {
if (!locked) {
locked = true
// open the link
} else {
alert("Locked")
}
}
//-->
</script>
As you can see I store the info in the var locked, but if you reload the page the variable will be initialized again.
xabi