使用jquery按需javascript调用的窗口卸载按需、窗口、jquery、javascript

2023-09-11 01:19:28 作者:你必须在、我不想一个人

我已经尝试了卸载和beforeunload在以下code结合,但没有浏览器发送一个请求:

I have tried for "unload" and "beforeunload" binding in following code, but none of the browser is sending a request:

$(window).bind('unload', function(){
       $.ajax({cache: false,
        dataType: "script",
            url: "test.php"
        });
    });

任何建议?

推荐答案

浏览器不等待这个code来完成...它运行得很好:)添加警报( '喜')来看看它运行。

The browser doesn't wait for this code to finish...it's running just fine :) Add an alert('hi') to see it running.

您所看到的问题是典型的卸载的问题,浏览器导航离开以及之前的请求得到你的情况......发了,这应该说真的,你正在运行没有code必须要完成,我可以移动到下一个页面之前。这是多么卸载的行为。您可以拨打电话同步保存用户那里,但请不要做到这一点。

The problem you're seeing is typical of unload issues, the browser navigates away well before a request gets fired off in your case...and it should really, no code you're running should have to complete before I'm allowed to move onto the next page. This is just how unload behaves. You could make the call synchronous to hold the user there, but please don't do this.

卸载活动是在窗口创建主要是你需要收拾东西的DOM周围留下来,更因此当垃圾收集的情况不是很好,因为它现在是(现在仍然是没有在IE浏览器)...这是不是真的设计与AJAX或考虑指标的跟踪。

The unload event was created on window mainly to clean up anything left around in the DOM you needed to, more-so when garbage collection wasn't as good as it is now (and still isn't in IE)...it wasn't really designed with AJAX or metric tracking in mind.