Welcome to WebmasterWorld Guest from 54.145.209.34

Forum Moderators: open

Tranform outgoing links to "confirm"

replaces all links in a page to confirm dialog box

   
5:46 pm on Mar 19, 2007 (gmt 0)

5+ Year Member



Hi all, I'm in need of a little assistance.

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]

7:44 pm on Mar 19, 2007 (gmt 0)

5+ Year Member



Just popped in to let you all know I got it working.

<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>

7:48 pm on Mar 19, 2007 (gmt 0)

10+ Year Member



Why not to use a real ANCHORS instead of to create own links array of the same name? ;) I mean DOM anchors collection : those A elements with their NAME attribute defined. So, add NAMEs to all your links outside content DIV, and the problem is over imho. (I bet you do not use NAME attr. for ordinary links in your pages? ;)
2:02 pm on Mar 20, 2007 (gmt 0)

5+ Year Member



All the links through out the entire site are dynamically generated. I have no control over them. The links in the main content area is user generated through a wysiwyg editor and I have no control over those either.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month