I'm using PHP/jQuery and have the following scenario:
I have a site (site1.php
) with a link, which points to another site (site2.php
), also I added to this link an ajax-onclick-event with jQuery which requests another site (ajaxcall.php
). This other site has "a lot of work" to do, so it's not a short request.
My goal is: Set the request to ajaxcall.php
in the background (asynchron) AND go immediately to site2.php
. (I do not need an answer of ajaxcall.php
)
My first implementation was like this:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#link").on("click",function(){
$.get('./ajaxcall.php');
</script>
</head>
<body>
<a href="./file2.php" id="link">link</a>
</body>
</html>
Obviously this won't work. Because the ajax-request (which is async), is aborted as soon as the page is changed.
So as far as I can see I have two possiblities here:
Make the request synchron & show a loading indicator (
$.ajax({url: './ajaxcall.php',async:false});
)
--> Disadvantage:file2.php
will not open before theajaxcall.php
is done.Open a popup (
window.open('ajaxcall.php')
) and make a synchron-ajax-request / or something similar there and auto-close it after that --> Advantage:file2.php
should open almost immediately --> (Big)Disadvantage: Popup??? A better way ???
I hope you understood what I'm trying to accomplish and that you can help me :)