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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statement involving link

Status
Not open for further replies.

kristinac13

Programmer
Dec 22, 2005
27
US
Let's say you can make the body of my page unload two ways - a button which does one thing, and a link which takes you somewhere else altogether. I want to write an if statement that says
If the unload is caused by the user clicking a link, do this...
If the unload is NOT caused by the user clicking a link, do the other...

Does that make sense? I seem to be having more trouble than I think I should be getting this to work but nothing I've tried so far has...Any ideas would be appreciated...
 
Use a boolean javascript variable to check for clicked status. If a link is clicked flag it to true, if not it stays false. When the page exits check the status of that boolean variable and do whatever you want based on that.
Code:
<script type="text/javascript">

linkClicked = false;

function setClickedValue() {
   linkClicked = true;
}

function leavingPage() {
   if (linkClicked) {
      //ignore the SNL juice reference
      alert("You lika da link??  Da link is good, eh??  I go get you more link");
   }
   else {
      alert("You didn't click the link.... What's the matter?  Isn't it good enough for you?");
   }
}

</script>

<body onunload="leavingPage()">
Time to go to <a href="[URL unfurl="true"]http://www.google.com"[/URL] onclick="setClickedValue()">Google</a>
</body>

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
of course, if you have a bazillion links on your page, you might want to attach the setClickedValue() function to them all dynamically...

Code:
<script type="text/javascript">

linkClicked = false;

function setClickedValue() {
   linkClicked = true;
}

function doCoolAttachThing() {
    var a = document.getElementsByTagName("a");
    for ( var i = 0; i < a.length; i++ )
        a[i].onclick = setClickedValue;
}

function leavingPage() {
   if (linkClicked) {
      //ignore the SNL juice reference
      alert("You lika da link??  Da link is good, eh??  I go get you more link");
   }
   else {
      alert("You didn't click the link.... What's the matter?  Isn't it good enough for you?");
   }
}

onload = doCoolAttachThing;



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Thank you so much for your responses. I think they're very close to what I need to do. Except I forgot to mention a catch...the links that I need to identify as being clicked are actually brought into the page through a <%headerstring%> type of arrangement. The header string is in a table cell. I tried to use the onClick event of the cell itself, but that only works if you click on the header somewhere OTHER than a link...I need to tell the page if any hyperlink is clicked, then do this cancel screen thing...otherwise load normally. Does that make my confusion clearer? :)
 
I tried the DoCoolAttachThing and it runs but for some reason it does not attach the onclick event to my anchor tags. I have an alert popping up to let me know it ran just fine, it tells me the exact number of links on the page, but it does not actually attach the "cancelScreen" (what I really want the onClick to do) to the a tags...Am I missing something? I can't see why this little function isn't working just fine...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top