Forum Moderators: open
Once my page has finished loading I have a little script that transforms all of the links into confirm dialog boxes. The idea is I don't want the user to lose the information he's entering in a complex form by mistake.
So far my little snippet works perfectly. My problem is, my main content area has links to help popups that I want to exclude from the script.
All of my main content is inside a
<div id="content"> MAIN CONTENT </div>
I want all links inside this container to be excluded in the script. What would I need to add to accomplish this?
Here is my script;
<script type="text/javascript">
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.onclick = function(){return wannaLeave(this);};
}
this.wannaLeave = function (x){
var answer = confirm ("Are you sure you want to leave?");
if (answer){
/* does stuff */
}
return false;
}
</script>
[edited by: Trace at 5:48 pm (utc) on Mar. 19, 2007]
<script type="text/javascript">
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
tmp = anchor;
link_center = false;
while (tmp.parentNode!= document) {
if (tmp.parentNode!= document && tmp.parentNode.id == 'main_content') {
link_center = true;
}
tmp = tmp.parentNode;
}
if(link_center) {continue;}
anchor.onclick = function(){return wannaLeave(this);};
}
this.wannaLeave = function (x){
var answer = confirm ("Are you sure you want to leave?");
if (answer){
/* does stuff */
}
return false;
}
</script>