Undo preventDefault () or the best way to programmatically disable link collections

I have a page with event listeners for network status. When the network is disconnected, I want to disconnect any cross-domains to go offline. I tried to use .preventDefault(), however, when the application returns to the network, I need to enable links again.

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 'untie'

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 will not cause a delay if the page has a significant number of links. Is there a simple call handling solution .preventDefault()or a better way to complete this task?

Possible solutions


href, /. HTML5 webdb hrefs onclick... , .

+5
1

, jQuery, , .

, $.click() .bind('click', ). , , :

var handler = function(event) { 
  event.preventDefault();
  console.log("the links, they do nothing!");
}

// when you want the external links to be inactive.
// you could use .click(handler) here too, it the same.
$('a[href^="http"]').bind('click', handler);

// when you want them back
$('a[href^="http"]').unbind('click', handler);

, href ^ = "http" , , . "http", , "ftp". , , Wikipedia "".

+9

All Articles