I have some links on a site which I need to direct to a page telling the user that they are leaving the site. All of these links already have an onClick function on them which normall triggers an alert telling them that they are leaving the site.
For example, this would normally trigger the alert:
<a href="http://www.example.com" onclick="return displayWarning(1, this);">Example</a>
What I'm trying to do is add functionality to that displayWarning() function so that if it is passed the integer '1' as a parameter, it takes the user to the new page such as: http://www.mySite.com/warning?url=http%3A//www.example.com
Right now I'm using this:
if(msg == 0) {
window.location.href='/warning?url='+($(this).href);
}
I have also tried this to see if I could just change them all at load, but seem to be experiencing the same problem:
$(document).ready(function(){
$("a[onClick='return displayWarning(1, this);']").attr('href', '/warning?url='+$(this).attr('href'));
});
However, url is not being properly defined as I'm fairly certain the the context of $(this) isn't set for the way that I'm trying to call it.
I've seen similar problems mentioned below but they don't seem to solve my issue as most of them are using either the class of the links or just changing all links to some value.
http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery http://stackoverflow.com/questions/1068385/jquery-attr-href-why-isnt-it-working
Thanks.