undo PreventDefault() 或以编程方式禁用链接集合的更好方法
我有一个页面,其中包含网络状态的事件侦听器。当网络“离线”时,我想禁用任何跨域链接以进入离线模式。我试图使用 .preventDefault()
,但是当应用程序重新上线时,我需要重新启用链接。
事件侦听器
//check network status
if(!navigator.onLine) {
offlineLinks(); //Offline mode function
}
//add event listeners for network status
window.addEventListener('offline', function(e) {
offlineLinks(); //Offline mode function
}, false);
window.addEventListener('online', function(e) {
//need to re-enable links/Online mode
}, false);
window.addEventListener('error', function(e) {
alert('Error fetching manifest: there is a good chance we are offline.');
offlineLinks(); //Offline mode function
});
“取消链接”功能
function offlineLinks() {
$('a[href^="http"]').click(function(e) {
e.preventDefault();
$('#offline-dialog').dialog({
modal: true,
buttons: {
Ok: function() {
$(this).dialog('close');
}
}
});
});
}
我正在寻找一种可扩展的解决方案,如果页面上有大量链接,该解决方案不会导致延迟。是否有一个简单的解决方案来反转 .preventDefault()
调用或完成此任务的更好方法?
可能的解决方案
我最初的想法是存储一个 href 值的数组,然后删除/添加。我一直在使用 webdb
来使用 HTML5 存储,我可以为其创建一个数据库并动态提取 onclick 的 href...但是我不确定这是否是最佳解决方案。
I have a page that has event listeners for the network status. When the network is 'offline' I want to disable any cross domain links as to go into an offline mode. I was trying to use .preventDefault()
, however when the app comes back online I need to re-enable the links.
Event Listeners
//check network status
if(!navigator.onLine) {
offlineLinks(); //Offline mode function
}
//add event listeners for network status
window.addEventListener('offline', function(e) {
offlineLinks(); //Offline mode function
}, false);
window.addEventListener('online', function(e) {
//need to re-enable links/Online mode
}, false);
window.addEventListener('error', function(e) {
alert('Error fetching manifest: there is a good chance we are offline.');
offlineLinks(); //Offline mode function
});
Function for 'de-linking'
function offlineLinks() {
$('a[href^="http"]').click(function(e) {
e.preventDefault();
$('#offline-dialog').dialog({
modal: true,
buttons: {
Ok: function() {
$(this).dialog('close');
}
}
});
});
}
I am looking for a scalable solution that won't cause lag if there are significant numbers of links on the page. Is there a simple solution to reverse the .preventDefault()
call or a better way to accomplish this task?
Possible Solutions
My initial thoughts were either having store an array of the href values and then remove/add. I have been playing around with the HTML5 storage using webdb
's I could create a database for and dynamically pull the hrefs onclick...however I'm not sure if this is the best solution for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在使用 jQuery,至少对于链接处理程序部分是这样。
需要意识到的是 $.click(handler) 只是 .bind('click', handler) 的简写。如果您在其他地方定义了处理程序,您也可以稍后取消绑定它,如下所示:
顺便说一句,如果您只希望这种情况发生在外部链接上,则 href^="http" 有点脆弱。内部链接可能以“http”开头,某些外部链接可能以“ftp”等其他协议开头。最好为此类链接提供自己的类,就像维基百科对“外部”所做的那样。
You seem to be using jQuery, at least for the link handler part.
The thing to realize is that $.click(handler) is just shorthand for .bind('click', handler). If you define the handler elsewhere, you can also unbind it later, like this:
By the way, href^="http" is a bit fragile, if you only want this to happen to external links. Internal links might start with "http", and some external links could start with other protocols like "ftp". Better to give such links their own class, like Wikipedia does with 'external'.