Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get target URL of clicked event - client side

I have to get the target URL when clicking on the menu or any link in the master page.

I have used the following code

var target = $(document.activeElement)[0].href;

Which works 90% correctly.Since when I click a link from a menu for the first time.It shows undefined.

That is I have Many Menus in the master page and each menu contains many navigations.When I click a navigation from a menu for the first time, it will not have the value.If I click any of the link from the same menu after that it shows correct target link.

Please help me to find a was to get the target URL.

like image 757
Akhil Avatar asked Mar 10 '23 06:03

Akhil


1 Answers

You can get the target url like this --

$('a').click(function(event) {
  $('div').html(event.target.href);
  event.preventDefault(); // Used to prevent the page from redirecting to google.com, just used for demo, you can remove it in your actual if not needed
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<a href="http://google.com">Link 1</a>
<a href="http://facebook.com">Link 2</a>
<a href="http://stackoverflow.com">Link 3</a>
<div></div>

Just give the function a parameter, something like e, and use the e to get the target url(href)

like image 165
Advaith Avatar answered Mar 19 '23 04:03

Advaith