Window.location error not caused by AJAX call

I have an AJAX click tracking function (calls WebMethod on the .aspx page) and I need to call it when the user clicks on the link.

Unfortunately, I use window.location = "newUrl" to change the page, which seems to cause the AJAX call to fail. Is there any way around this?

I do not need to get any information from an AJAX call, I just need to make sure that WebMethod is called.

I know that I could just redirect success() or failure() calls, but then I would have to wait for the clickTracking() method to execute, which takes ~ 1s . This is not acceptable by agreement in the design specification, and therefore it is not a viable solution.

+6
javascript jquery ajax pagemethods
source share
3 answers

Instead of using AJAX, why don't you just send the user to an ASPX page that records the click and then sends the location header to redirect to the new URL? For example, when you click a link, the following will happen:

 window.location = "/redirect.aspx?track=true&url=" + encodeURIComponent(newUrl); 

redirect.aspx will handle both tracking and redirecting to the new URL. This is almost the only way to do this without waiting for the AJAX request to complete before sending the user along the way.

See also: Redirecting Users to Another Page (MSDN)

It is probably worth noting that you see this method every day on many sites. Most often they are found in ads. The AdSense sponsored link is as follows:

 http://www.google.com/aclk ?sa=L&ai=C6DnFKnl1S5zhKoSBjAeqyKzKBPOD_2ehlIXfD9WsvQsQAVDQzK______ 8BYLvOuYPQCsgBAaoEGU_Q8k4OJin9jMDXAD6KQ_2Dsv7xyokLxRY &num=1&ggladgrp=13269254452699041863&gglcreat=8511532373602616220 &sig=AGiWqtwzFm2sTimBuQpkD5kazyRNkkqH3w &q=http://www.amazon.co.uk/s/%3Fie%3DUTF8%26keywords%3Dhello%2Bworld 

ish, anyway (I knocked it a bit). When you click on it, you will land at http://www.google.com/aclk , which redirects you to the final site after tracking the click.

+3
source share

I just need to make sure WebMethod is called.

If this is really important, you can do the following:

 function onLinkClicked() { $.ajax({ url: 'WebMethod.aspx', success: function(data) { window.location = "newUrl"; } }); } 

When redirecting the page there will be some timeout. This may or may not be acceptable, depending on your application and the average delay from your users to your host.


EDIT:

In addition to the new comment below, unfortunately, changing window.location will abort your AJAX request.

For internal links:. You can track your links as soon as users land on a new page, and not when they click on a hyperlink.

For external links: I would use a server-side redirect method, as Andy E suggested in another answer .

+2
source share

Use the <iframe> wrapper to wrap your pages. Then, when you have an AJAX request, pass the request to the shell and go to <iframe> . Doing this will give you the opportunity to have a completely asynchronous request, while maintaining application responsiveness.

Of course, you also do not change the address bar and effectively kill your SEO, but this is a compromise for having a bad architecture. The fact that click tracking takes about a second is just awful. Perhaps you should run it on an asynchronous thread on the server and just transfer the data to the client in a separate thread ( System.Threading ). This will alleviate the problems you are facing.

Otherwise, you may have asynchronous , reliable, or not hacked together . Choose any two.

EDIT

Not bad, you can also run your AJAX request as a popup:

 window.open(url,'ajax','height=0,width=0'); 

This is not a good idea, but it will work.

0
source share

All Articles