I want to control whether a link is clickable or an error should be displayed (Based on result of an ajax call).
<a class="lnkCustomer" href="http://localhost/viewcustomer" target="_blank" data-customerno="237">View</a>
I get to the point where I am able to set the link as "allowed to be clicked":
// Authorized
anchor.data("authorized", true);
However when I run this code, the link still does not open. Ideally once the ajax call is complete, it should invoke the click event. I believe the issue is in this line.
// Trigger Anchor
anchor.click();
This is the entire code:
<script type="text/javascript">
$(".lnkCustomer").click(function(e)
{
var customerNo = $(this).data('customerno');
var anchor = $(this);
// Check for authorized
if (anchor.data("authorized"))
{
return true;
}
$.ajax(
{
url: 'http://localhost/checkcustomer',
type: 'POST',
dataType: 'json',
data: { customerNo: customerNo },
cache: false,
success: function (result)
{
if (result.success)
{
// Authorize
anchor.data("authorized", true);
// Trigger Anchor
anchor.click();
}
else
{
// Show a message in a alert or div
alert('Not authorized');
}
}
});
// Default to false (Do not process anchor)
return false;
});
</script>
Notes: I am using class instead of id in the anchor because I have various links that will trigger this event. However as you can see, this should not be an issue since I am always referring to the individual object:
var anchor = $(this);
7
Answers
Unfortunately, as others mentioned, href cannot be delayed. I did find a work-around to suit the particular scenario. I created an intermediary page when user clicks on the href. This page then performs the ajax request (server side), if it is validated it goes on and display the resource. Otherwise it displays an error and stays on the intermediary page.
I think we cannot overwrite the default behavior of the anchor tag but we can work around it. In this solution, I have replaced
href
withdata-link
. And mimic the anchor mechanism withwindow.open
.Code :
Please note :
OR add a "show links" button and then fetch only accessible links to the user. You can do it via ajax. and also you will not need this JS/Jquery code.
OR assign a random number to data-link and then fetch in your PHP code see if it is authorized if it is then only return accessible HTTP link. many ways to improve.
preventDeault
, but it do not workAnytime you want to override a browsers default action, you need to call
.preventDefault()
at the top of the event listener.After that, since you’re server side validating the link every time it’s clicked, there’s really no reason to store it’s state client side. Instead of trying to re-click the link, you could just call
window.open()
, which is what achors do by default.Try triggering click like this:
anchor[0].click();
and see if that works.For readability, you can save a reference to DOM element of anchor, not just the jQuery object:
and trigger click using the DOM element:
You cannot open a new tab without popups enabled or the user’s click for that event.
You cannot delay it with promises or invoke a trusted click event.
If you want to verify a user can click your link, then do the API request on page load and store the result.
Or make your link into a button with a two click process for checking and then opening.
AFAICT you are 90% of the way there, you’re just missing a few key details.
Working JSFiddle.
e.preventDefault()
: As already mentioned in other answers/comments, you need to prevent the default action which the event triggers, otherwise the browser will just begin the process of navigating to the link while your JS is still running.anchor.click()
will trigger a click on your link, which will … start the whole process again! You’ll get stuck in a recursive loop. The click is done, you now want to navigate. To open a new window in Javascript, usewindow.open(href, '_blank');
If your link is already authorised, you need to do the same as if the AJAX authorises it the first time around. Instead of
return true;
, you need to do the samewindow.open()
again.Also a suggestion – the convention for using GET or POST is:
AFAICT you are simply querying some customer details, so this should be a GET request.
I’ve modified you code to work with JSONPlaceholder – a dummy REST API which provides JSON data for this kind of situation (where we need to test an AJAX call with a working response), so we can simulate your AJAX call. It only accepts GET requests, so I’ve changed your POST to GET, and I’ve updated the test of the response based on the dummy data it sends back. You can see the output we’re working with for User ID 1, and User ID 2.
Since you need to do the "go to the link" chunk of code in more than one place, I extracted that to a function you can call, so we don’t need to duplicate that code in several places.
I also added a few extra alerts so you can be sure of what is happening.
See it all in action on JSFiddle.
Javascript:
HTML:
There is a simple answer : you can’t trigger a click if it doesn’t come from a trusted event (change, click, dblclick, mouseup, reset, submit).
Here you are trying to trigger a click after an AJAX (asynchronous) request, which is not allowed.
More info here.
As suggested you could replace it by
window.open(href, '_blank');
but beware it could also be blocked by the browser parameters.