marco
marco

Reputation: 1140

event.preventDefault does not seem to work....why?

Working in local, so I cannot give you any link but I will try to explain the situation.

I'm doing a wordpress website and I'm creating ajax requests to make any internal link on the site will load into the main content area of the main page.

Here is the entire script that I'm using

jQuery(document).ready(function($) {
    //individua il container in cui sarà caricata la pagina
    var $mainContent = $("#main-wrapper"),
        //individua l'url "madre"
        siteUrl = "http://" + top.location.host.toString(),
        url = '';

    $(document).on("a[href^='"+siteUrl+"']:not([href*=/wp-admin/]):not([href*=/wp-login.php]):not([href$=/feed/])", "click", function(event) {
        location.hash = this.pathname;
        if ( event.preventDefault ) {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    });

    $(window).bind('hashchange', function(){
        url = window.location.hash.substring(1); 
        if (!url) {
            return;
        } 
        url = url + " #content"; 
        $mainContent.animate({opacity: "0.1"}).html('<p>Please wait...</>').load(url, function() {
            $mainContent.animate({opacity: "1"});
        });
    });

     $(window).trigger('hashchange');
})

In a few words, you can see how I'm trying to "disable" the link: I want to avoid the loading of the link to the external page

The error that I receive in the console is this:

event.returnValue is deprecated. Please use the standard event.preventDefault() instead.

How can I fix it?

Upvotes: 0

Views: 161

Answers (1)

jayarjo
jayarjo

Reputation: 16754

I believe event type should come first in your on binding and then the selector:

$(document).on("click", "a[...])", function(event) ...

Upvotes: 5

Related Questions