homepage Welcome to WebmasterWorld Guest from 54.161.246.212
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Tranform outgoing links to "confirm"
replaces all links in a page to confirm dialog box
Trace

5+ Year Member



 
Msg#: 3286501 posted 5:46 pm on Mar 19, 2007 (gmt 0)

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]

 

Trace

5+ Year Member



 
Msg#: 3286501 posted 7:44 pm on Mar 19, 2007 (gmt 0)

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>

Moby_Dim

10+ Year Member



 
Msg#: 3286501 posted 7:48 pm on Mar 19, 2007 (gmt 0)

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? ;)

Trace

5+ Year Member



 
Msg#: 3286501 posted 2:02 pm on Mar 20, 2007 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved