Sachin
Sachin

Reputation: 1698

Why a JavaScript function with some specific name is not working while others are?

A very interesting problem I am facing these days is regarding one of my JavaScript function. My JavaScript function with some specific name is not working but if I change its name to anything else then it is working. Have a look -

// function to retain the jquery ui css for toolbar
function retain_css() {
    alert('hi');
    $( "#new_sort_options" ).buttonset();
}

// new sort
$(document).on("click", ".new_sort_button", function() { 
    var order       = $(this).val();
    var make_id     = $('#new_make_id').val();
    $.ajax({
        beforeSend  : start_loader(),
        type        : 'POST',
        url         : '/ajax/new-sort.php',
        data        : 'order='+order+'&make_id='+make_id,
        dataType    : 'json',
        success     : function(data) {
                        $("#new_results_toolbar").html(data.toolbar);
                        $("#new_results").html(data.models);
                        retain_css();
                        end_loader();
        }
    }); 
});

But retain_css() is not working at all. Even alert() is not firing. But if i change its name to anything such as my_fun() then the code works. I don't understand why it is happening so? Any idea? Don't worry about end_loader() function as it has nothing to deal with my problem. I also changed the order of code when retain_css() was being used but didn't work.

Upvotes: 0

Views: 91

Answers (2)

user4152928
user4152928

Reputation:

Some functions are already programmed like 'alert('hi');', that is a function called alert:

function alert() {
    /* do something */
} 

That function also doesn't work.

Upvotes: 0

Radek Pech
Radek Pech

Reputation: 3098

Try not to create global functions because it may collide with other frameworks or libraries.

//define private namespace
window.user3779493Functions = {};
//define method
user3779493Functions.retain_css = function() { ... }
//call method
user3779493Functions.retain_css();

Upvotes: 1

Related Questions