Search code examples
javascriptmootools

Unexpected syntax error in MooTools code


Im creating a e-mail overlay and running into a small problem with a syntax error I just cant figure out. Im using MooTools and Mediabox as my tools. Not super well versed in Javascript.

Here is the code that is plopped in the template page. Checks for a cookie that I will be setting after first time visitors close the box. "mbCloseLink" is the id on the close button for the overlay. I want to listen for a click event and inside of that function is where I would set the cookie. The funny thing is, the code works if I remove it from the conditional statement. Im assuming this has something to do with "mbCloseLink" not being loaded by the time something is getting fired off but I dont have enough JS experience to figure that out.

The error is: Uncaught SyntaxError: Unexpected token } overlay.js line 13

window.addEvent('load', function () {

var mc_cookie = Cookie.read('mc');
console.log('cookie value = ' + mc_cookie);

if (mc_cookie !== "x") {
    Mediabox.open('overlay.php','Overlay','417 350');
    $('mbCloseLink').addEvent('click', function() {
        console.log('Closed box.');
        var ck = Cookie.write('mc', 'x');
        console.log('Wrote cookie.');
    }
 }
});

Solution

  • Change

    $('mbCloseLink').addEvent('click', function() {
        console.log('Closed box.');
        var ck = Cookie.write('mc', 'x');
        console.log('Wrote cookie.');
    }
    

    to

    $('mbCloseLink').addEvent('click', function() {
        console.log('Closed box.');
        var ck = Cookie.write('mc', 'x');
        console.log('Wrote cookie.');
    }); // <==== here