Solved

How can i capture anchor clicks to navigate the page? (preferrably in jQuery)

Posted on 2009-05-19
5
315 Views
Last Modified: 2012-05-07
It's really frustrating because i forgot to save in my favourites a video tutorial to do exactly this. I want to capture all attempts to navigate away from the page (via an anchor i.e. <a href="otherpage.html">), and only navigate them depending on some condition (which is only if they don't have unsaved changes on that page).

I remember the example i saw involved getting all the anchors on the page, and then overriding the "open" function? Would that make sense? Does an anchor have an "open" function, or just an onclick function??

The overridden function then performs the check, and returns true or false depending on whether or not the page should be allowed to navigate. Can anyone refresh me on how i would do this? .. or does any one have any other similar solutions?

Ideally solutions in jQuery, but anything in plain javascript will do.

0
Comment
Question by:paddycobbett
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24428955
<body onLoad="window.leaving=true" onBeforeUnload="return (leaving) ?'Are you sure':null">
<a href="leaving.html">Leaving</a>

<a href="staying.html" onClick="leaving=false">Leaving</a>

or the opposite


 
<body onLoad="window.leaving=false" onBeforeUnload="return (leaving) ?'Are you sure':null">
<a href="leaving.html" onClick="leaving=true">Leaving</a>

<a href="staying.html" >Leaving</a>
 
0
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 24430897
try this
$("a").each({
this.onclick=function()
{
   var goThere=confirm("You wana go out?");
   if (!goThere) return false;
}
})

Open in new window

0
 
LVL 18

Accepted Solution

by:
Pawel Witkowski earned 500 total points
ID: 24430903
Or just simply:
$("a").click(function()
{
   if (!confirm("You wana go out?")) return false;
}
);

Open in new window

0
 
LVL 1

Author Comment

by:paddycobbett
ID: 24434085
This morning having gone through my whole browser history i finally found the link to the video tutorial i was looking for! And yes, it was exactly as you stated :)

thanks for your response
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24438736
If that works for you then why not just
   return confirm("You wana go out?");
 
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question