0

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.

7
  • 1
    Wow, how is that possible? Any chance you can replicate this in a live example (a snippet here or jsfiddle, etc.)? Commented Oct 17, 2014 at 11:47
  • "Don't worry about end_loader() function as it has nothing to deal with my problem" Then remove it from the code that you show us. Commented Oct 17, 2014 at 11:48
  • Any errors in console? Can you abstract away the AJAX? Make a testcase and show us the results of your debugging. Commented Oct 17, 2014 at 11:49
  • The only way this is possible is if retain_css is declared again further down or somewhere else and is overwriting the function. Commented Oct 17, 2014 at 11:51
  • retain_css works perfect for me, so your error may be above the code that you´re showing us. Please paste part of your code that is above the retain_css(), or just a fiddle. Commented Oct 17, 2014 at 11:51

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

That function also doesn't work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.