Search code examples
javascriptphpwordpressjquery-isotopejquery-cookie

'return false' statement outside function error


I've been trying to implement the following Javascript code into the head of a Wordpress project. I found the original method here: jQuery isotope query filtering results for url but the last 'return false;' keeps throwing the error 'return not in function'. Admittedly, I had to update the script to use jQuery instead of the $'s in the original script & have included the jquery.cookie.js as they had it in their working example (as think it's old) but wondered if anyone could suggest a workaround for the return false error so I can get this concept to work with jQuery? Thanks

<script>
// filter items when filter link is clicked
		jQuery(document).ready( function() {
		jQuery("#filters a").click(function(){
			var selector = jQuery(this).attr('data-filter');
		jQuery('#portfolio').isotope({ filter: selector });
			return false;
		});
		});
</script>
<script>
		// Set cookie based on filter selector
		jQuery("#cookiefilter a").click(function(){
			var selector = jQuery(this).attr('data-filter');
			$.cookie("listfilter", selector, { path: '/projects/' });
		});	
		if ( $.cookie("listfilter") ) {
			jQuery('#portfolio').isotope({ filter: $.cookie("listfilter") });
			$.cookie("listfilter", null, { path: '/projects/' });
			return false;
		}
		
</script>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
</script>
</head>


Solution

  • The error is correct. Your "return false" occurs outside a function, which is not allowed.

    <script>
        // Set cookie based on filter selector
        jQuery("#cookiefilter a").click(function(){
            var selector = jQuery(this).attr('data-filter');
            $.cookie("listfilter", selector, { path: '/projects/' });
        });    /****END OF FUNCTION****/
    

    The function() ends here. From here onwards, you are outside a function:

        if ( $.cookie("listfilter") ) {
            jQuery('#portfolio').isotope({ filter: $.cookie("listfilter") });
            $.cookie("listfilter", null, { path: '/projects/' });
            return false;
        }
    

    Perhaps you ended the function early by mistake.
    If you move the line marked END OF FUNCTION to the end of that <script>, the error should be fixed.