如何取消浏览器的导航已在使用JavaScript的进展 [英] How to cancel browser navigation that is already in progress with JavaScript

查看:103
本文介绍了如何取消浏览器的导航已在使用JavaScript的进展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不相信这是可能的,但想我给它一个镜头。

I don't believe this is possible but thought I'd give it a shot.

作为该我工作的一个项目的一部分,是在不同的网站,用户可以通过标准锚链接导航到一个页面。目标页面响应有时会比较慢,而且客户希望把一个超时的地方,这样,如果时间太长导航到目的地的页面,取消浏览器导航并显示一条消息。

As part of a project that I'm working on, there is a page on a different site that a user can navigate to via a standard anchor link. The destination page is sometimes rather slow to respond and the client wants to put a "timeout" in place so that if it is taking too long to navigate to the destination page, to cancel the browser navigation and show a message.

是否有可能使用Javascript取消浏览器的导航是已在进行中(即到目标站点的请求已作出)?如果是这样,怎么样?

Is it possible with Javascript to cancel a browser navigation that is already in progress (i.e. the request to the destination site has already been made)? If so, how?

推荐答案

我进行了简单的测试,这是可能的。我用PHP来进行测试,但如果你使用不同的语言,这将不再是一个问题。底线是JavaScript可以做到这一点在 cancel.php 看到。请参阅样本code:

I conducted a simple test, it is possible. I used PHP to conduct the test but this will not be an issue if you use a different language. The bottom line is JavaScript can do it as seen in cancel.php. Please see the sample code:

<?php
sleep(30);
echo 'hello';
?>

cancel.php

<h1>hello?</h2>
<script type="text/javascript">
location.href='sleep.php';
setTimeout(function(){
    location.href='#';
    alert('it is taking too long to respond, probably the site is down!');
}, 10000);
</script>

运行cancel.php后,它会重定向到sleep.php。加载时间10秒后,它将停止加载sleep.php,并得出结论,该网站已关闭。

After running cancel.php, it will redirect to sleep.php. After 10 seconds of loading time, it will stop loading sleep.php and conclude that the site is down.

这篇关于如何取消浏览器的导航已在使用JavaScript的进展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆