問題描述
我正在從我們公司內部網的各個頁面實現點擊跟蹤,以添加一些急需的眾包流行鏈接功能(過去 24 小時內您部門中最流行的鏈接"等)
I'm implementing click tracking from various pages in our corporate intranet in order to add some sorely needed crowd-sourced popular link features ("most popular links in your department in the last 24 hours", etc.)
我正在使用 jQuery 的 .live() 為頁面上的所有鏈接元素綁定到 mousedown 事件,過濾事件,然后在返回之前向后端服務器發出帶有各種數據的偽 ajax 請求true 以便觸發鏈接操作:
I'm using jQuery's .live() to bind to the mousedown event for all link elements on the page, filter the event, and then fire off a pseudo-ajax request with various data to a back-end server before returning true so that the link action fires:
$("#contentarea a").live("mousedown", function(ev) {
//
// detect event, find closest link, process it here
//
$.ajax({
url: 'my-url',
cache: false,
dataType: 'jsonp',
jsonp: 'cb',
data: myDataString,
success: function() {
// silence is golden -- server does send success JSONP but
// regardless of success or failure, we allow the user to continue
}
});
return true; // allow event to continue, user leaves the page.
}
正如您可能從上面猜到的那樣,我有幾個限制:
As you can probably guess from the above, I have several constraints:
- 后端跟蹤服務器與調用頁面位于不同的子域中.我無法解決這個問題.這就是為什么我使用 JSONP(和 GET)而不是正確的 AJAX 和 POST.我無法實現 AJAX 代理,因為 Web 服務器沒有腳本的出站網絡訪問權限.
- 這可能無關緊要,但為了全面披露,內容和腳本位于主要內容"iframe 內(這不會改變.我可能最終會將事件偵聽器移至父框架監視它的鏈接和所有子內容,但第 1 步是讓它在1 個子窗口"的簡化情況下正常工作).父子是同一個域.
- 后端是 IIS/ASP(同樣是一個約束——別問了!),所以我不能立即分叉后端進程或以其他方式終止響應,但我可以像在更好的平臺
盡管如此,但在大多數情況下,該系統仍能正常工作——我點擊頁面上的鏈接,它們會非常無縫地出現在數據庫中.
Despite all this, for the most part, the system works -- I click links on the page, and they appear in the database pretty seamlessly.
但它并不可靠——對于大量鏈接,尤其是目標設置為_top"的非站點鏈接,它們不會出現.如果鏈接在新選項卡或窗口中打開,則注冊成功.
However it isn't reliable -- for a large number of links, particularly off-site links that have their target set to "_top", they don't appear. If the link is opened in a new tab or window, it registers OK.
我已經排除了腳本錯誤——似乎是:
I have ruled out script errors -- it seems that either:
(a) 請求永遠不會及時到達后端;或
(a) the request is never making it to the back-end in time; or
(b) 請求正在發出,但 ASP 檢測到客戶端在不久之后斷開連接,并且由于它是 GET 請求,因此沒有處理它.
(b) the request is making it, but ASP is detecting that the client is disconnecting shortly afterwards, and as it is a GET request, is not processing it.
我懷疑 (b),因為服務器的延遲非???,而且許多鏈接注冊正常.如果我在事件觸發后彈出警報,或者將返回值設置為false,則點擊注冊成功.
I suspect (b), since latency to the server is very fast and many links register OK. If I put in an alert pop-up after the event fires, or set the return value to false, the click is registered OK.
關于如何解決這個問題的任何建議(在我無法更改約束的情況下)?我無法使 GET 請求同步,因為它不是真正的 AJAX.
Any advice on how I can solve this (in the context that I cannot change my constraints)? I can't make the GET request synchronous as it is not true AJAX.
問:如果我向 ASP 發出 POST 請求會更好嗎?如果 (b) 是罪魁禍首,那么 POST 與 GET 的行為會有所不同嗎?如果是這樣,我可以使用隱藏的 iframe/form 來發布數據.但是,我懷疑這會更慢,更笨重,并且可能仍然無法及時完成.因為是跨域的,所以我無法監聽請求是否完成.
Q: Would it work better if I was making a POST request to ASP? If (b) is the culprit would it behave differently for POST vs GET? If so, I could use a hidden iframe/form to POST the data. however, I suspect this would be slower and more clunky, and might still not make it in time. I wouldn't be able to listen to see if the request completes because it is cross-domain.
問:我可以在 GET 請求觸發后向腳本添加延遲嗎?如何以單線程方式執行此操作?我需要從我的函數中返回 true,以確保最終觸發默認事件,所以我不能使用 setTimeout().等待成功"觸發并設置一些變量的緊密循環會起作用嗎?我擔心這會使事情凍結太多,響應速度會變慢.我假設 jQuery delay() 插件也只是一個循環?
Q: Can I just add a delay to the script after the GET request is fired off? How do I do this in a single-threaded way? I need to return true from my function, to ensure the default event eventually fires, so I can't use setTimeout(). Would a tight loop waiting for 'success' to fire and set some variable work? I'm worried that this would freeze up things too much and the response would be slowed down. I assume the jQuery delay() plugin is just a loop too?
或者是我沒想到的其他東西可能是罪魁禍首?
Or is something else I haven't thought of likely to be the culprit?
我不需要萬無一失的可靠性.如果所有鏈接在 95% 的時間里都是同樣可捕獲的,那就沒問題了.但是現在,有些鏈接在 100% 的時間里都可以捕獲,而另一些則無法捕獲——這并不能達到我想要實現的目標.
I don't need bullet-proof reliability. If all links are equally catchable 95% of the time it is fine. However right now, some links are catchable 100% of the time, while others are uncatchable -- which isn't going to cut it for what I want to achieve.
提前致謝.
推薦答案
解決了!
簡短的回答是:沒有可靠的方法通過 GET 請求進行跨域.我嘗試了各種方法,包括存儲事件并嘗試稍后重播事件,以及各種黑客嘗試讓它發揮作用.
The short answer is: there is no reliable way to do this cross-domain with a GET request. I tried all sorts, including storing the event and trying to replay the event later, and all manner of hacks to try to get that to work.
然后我嘗試了緊循環,但它們也不可靠.
I then tried tight loops, and they weren't reliable either.
最后,我只是放棄并使用了一個動態創建的表單來發布結果,目標設置為隱藏的 iFrame.
Finally, I just gave in and used a dynamically created form that POSTed the results, with the target set to a hidden iFrame.
那 工作可靠——瀏覽器似乎在繼續之前暫停以完成其 POST 請求,并且 ASP 尊重 POST.事實證明,它一點也不笨拙".當然,由于瀏覽器安全模型,我看不到結果……但在這種情況下沒關系.
That works reliably -- it seems the browser pauses to finish its POST request before moving on, and ASP honours the POST. Turns out it's not 'clunky' at all. Sure, due to the browser security model I can't see the result... but it doesn't matter in this case.
我現在很自責,因為我沒有先嘗試那個選項.
I am now kicking myself that I didn't try that option first.
這篇關于JavaScript/jQuery:如何在用戶離開頁面之前確??缬螯c擊跟蹤事件成功?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!