Solved

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

Posted on 2009-05-19
5
316 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

828 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