Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery 1.9, JS - function is not defined in Chrome

This is really puzzling me. I have function defined in main.js file, which is loaded in the header, and then latter on I am calling that function at the end of HTML code. In Chrome I am getting error Uncaught ReferenceError: delete_image is not defined, but in Firefox it is working normally (same error is appearing in Opera). What is going on?

Function:

function delete_image(button, data = false){
    button.on('click', function(){
    var $this = $(this),
    url = $(this).attr('href');

    if( data == 'tmp' )
    {
        data = 'id=' + $this.data('id');
    }
    else if ( data == true )
    {
        data = forma.serialize();
    }

    $.confirm({
        'title'     : 'Image Delete',
        'message'   : 'Do you want to delete this image?',
        'buttons'   : {
            'Yes'   : {
                'class' : 'blue',
                'action': function(){
                    $.post(url, data,  function(){
                        $this.parent().slideUp('slow');
                    });
                }
            },
            'No'    : {
                'class' : 'gray',
                'action': function(){}
            }
        }
    });
    return false;
    });
};

Calling the function (after HTML part of the code):

<script>
    var link = $('a[role=delete]');
    delete_image(link);
</script>
like image 210
Sasha Avatar asked May 24 '13 14:05

Sasha


1 Answers

You can't pass a default argument value in standard javascript today, this is invalid in Chrome :

function delete_image(button, data = false){

What you should do is

function delete_image(button, data){
    if (data == undefined) data=false;

The MDN precises in what browsers you can use this feature (answer : only Firefox).

This should be available with Harmony. Here's some detail about ES6's plan.

like image 154
Denys Séguret Avatar answered Nov 07 '22 02:11

Denys Séguret