简体   繁体   English

如何取消超链接点击?

[英]How can you cancel a hyperlink click?

I am building an offline-capable HTML 5 web app for subway users. 我正在为地铁用户构建具有离线功能的HTML 5 Web应用程序。 Often, mobile networks are so slow on the subway that page requests take a minute or more to get a response. 通常,地铁上的移动网络非常慢,以至于寻呼请求需要一分钟或更长时间才能获得响应。 I would like to offer users the option of switching to offline mode if loading a page takes more than 10 seconds. 如果页面加载时间超过10秒,我想为用户提供切换到离线模式的选项。

I have tried to do this with a javascript setTimeout() just before setting window.location to a new page. 我试图在将window.location设置为新页面之前使用javascript setTimeout()进行此操作。 If the page is still around when the timeout function is called, this means the page is taking too long, and I prompt the user with a confirm() dialog. 如果在调用超时功能时页面仍在周围,则意味着该页面花费的时间太长,并且我会向用户显示一个Confirm()对话框。 If the user taps OK, I set window.location = '/offline', which is served out of appcache. 如果用户点击“确定”,我将设置window.location ='/ offline',这将从应用程序缓存中提供。

This works great most of the time, but sometimes the response from the original hyperlink comes back while the confirm dialog is on the screen. 这在大多数情况下都有效,但是有时在确认对话框出现在屏幕上时,原始超链接的响应会返回。 On both iOS and Android the presence of the confirm dialog seems to block the original hyperlink from replacing the page. 在iOS和Android上,确认对话框的出现似乎都阻止了原始超链接替换页面。 On iOS, however, dismissing the confirm dialog always takes you to the original page, not the offline page. 但是,在iOS上,取消确认对话框始终会将您带到原始页面,而不是离线页面。

I could probably replace the confirm dialog with a similar floating HTML dialog box on the page, but this would not give the user a change to respond at all if the page comes back while the dialog is up. 我可以用页面上的类似浮动HTML对话框替换“确认”对话框,但是如果对话框打开时页面又返回,则根本不会给用户任何更改以做出响应。

What I really want to do is cancel the hyperlink click. 我真正想做的是取消超链接单击。 But this seems impossible. 但这似乎是不可能的。

Is there any other way to achieve the desired effect? 还有其他方法可以达到预期的效果吗?

There is no requirement for the old page to respond in any way once navigation to another page has started, so the fact that it works at all is non-standard and kind of haxy. 一旦导航到另一个页面,就不需要旧页面以任何方式做出响应,因此,完全可以正常工作的事实是非标准的,并且有些混乱。

You might be able to request the new page via AJAX, which is cancellable (either with .abort() or by simply ignoring the response). 您可能可以通过可取消的AJAX请求新页面(通过.abort()或通过简单地忽略响应)。 Then when it loads you can use innerHTML to dump the whole thing over the current page. 然后,在加载时,您可以使用innerHTML将整个内容转储到当前页面上。 Again, though, this is somewhat haxy and unreliable (stylesheets, scripts etc may not work). 同样,这有点模糊且不可靠(样式表,脚本等可能不起作用)。

All in all, attempting mess with network connectivity in this way is going to be as unreliable as the connection itself. 总而言之,以这种方式尝试破坏网络连接将与连接本身一样不可靠。

You can replace you confirm function with a jQuery dialog. 您可以使用jQuery对话框来替换确认功能。 In the jQuery dialog, you can have a link pr a button that directs the user to the offline page. 在jQuery对话框中,您可以有一个链接pr a按钮,该按钮将用户定向到脱机页面。 If the user clicks on the link/button in time, the user will be directed to offline page. 如果用户及时单击链接/按钮,则该用户将被定向到脱机页面。

Link for jQuery dialog sample: http://jqueryui.com/dialog/#modal-message jQuery对话框示例的链接: http : //jqueryui.com/dialog/#modal-message

On WebKit browsers, you can use window.stop() to cancel everything that the browser is loading. 在WebKit浏览器上,可以使用window.stop()取消浏览器正在加载的所有内容。 This will cause the browser to terminate the request for the next page (as well as any other requests downloading images, javascript, ajax, etc.) 这将导致浏览器终止对下一页的请求(以及下载图像,javascript,ajax等的任何其他请求)。

I solved the problem with the slow connection dialog disappearing if the page finally loads through design. 如果页面最终通过设计加载,我解决了慢速连接对话框消失的问题。 Instead of a dialog, I made it a ribbon overlaid across the middle of the page, so it was less jarring to users if the ribbon and page disappeared if and when the page response finally came through. 我用对话框代替功能区,而不是使用对话框,而是在页面中间覆盖了一条功能区,这样,如果在页面响应最终通过时功能区和页面消失了,对用户的影响就较小。 The wait option below simply makes the ribbon disappear. 下面的等待选项只是使功能区消失。 Here's the code that works on Webkit browsers: 以下是可在Webkit浏览器上使用的代码:

    // This code executes in the click event for a link to forUrl
    clearTimeout(MyApp.slowPageTimeout);
    MyApp.slowPageTimeout = setTimeout('MyApp.slowPageLoadEvent("'+forUrl+'");', 10000);

    slowPageLoadEvent: function(forUrl) {           
        var cachedDocument = myApp.getDocumentByUrl(forUrl);            
        var height = 100;
        var position = window.innerHeight/2+window.scrollY-height/2;
        var slowHttp = "<div id='slow-http-warning'>Slow connection.  <span id='no-wait'>Read offline</span>, or <span id='wait'>wait</span>?</div>";

        if ($("#slow-http-warning").length == 0) {
            $('body').append(slowHttp);                         
        }
        else {
            $('#slow-http-warning').html(slowHttp);                     
        }
        $("#slow-http-warning").css('top', ""+position+"px");
        $("#wait").click(function() {
            $('#slow-http-warning').remove();
        });
        $("#no-wait").click(function() {
            window.stop(); // not supported in IE, but it has an equivalent: document.execCommand("Stop");
            $('#slow-http-warning').remove();
            MyApp.setOffline();  // switch to operating in offline mode
            MyApp.go(cachedDocument); // display page from local storage cache
            return;           
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM