Dmytro
Dmytro

Reputation: 17196

jQuery crash because of bundle minification in release mode

I have jquery bundle:

bundles.Add(new ScriptBundle("~/bundle/jquery").Include(
                ScriptsPath("jquery-2.0.3.js"),
                ScriptsPath("jquery.validate.js"),
                ScriptsPath("jquery.validate.unobtrusive.js"),
                ScriptsPath("jquery-ui-1.10.3.js"),
                ScriptsPath("jquery.validate.unubtrusive.config.js"),
                ScriptsPath("jquery.easing.1.3.js "),
                ScriptsPath("jquery.unobtrusive-ajax.min.js"),
                ScriptsPath("jquery.validate.custom.attributes.js") ...

On user registration page I have both login and register forms so form inputs have Register. and Login. prefixes in their names. Basically it looks like:

<input type="text" ... id="Register_Email" name="Register.Email" />
<input type="password" ... id="Register_Password" name="Register.Password" />

When I publish my application in release mode i get this error in bundle file:

uncaught jquery error in bundle file

This is obviously because of the dot in input's name. How can I save the dot and fix this issue? I've already tried to BundleTable.EnableOptimizations = false; but it didn't help and I do not consider this as appropriate solution because it annihilates the purpose of the bundle. Also note that issue takes place only in Release mode.

EDITS: Bundle file list contains one script file of my own, it contains client side validation logic for my ForbidHtmlAttribude:

jquery.validate.custom.attributes.js

jQuery.validator.unobtrusive.adapters.add(
    'forbidhtmlattribute',
    ['htmlregexpattern'],
    function (options) {
        options.rules['forbidhtmlattribute'] = options.params;
        options.messages['forbidhtmlattribute'] = options.message;
    }
);

jQuery.validator.addMethod('forbidhtmlattribute', function (value, element, params) {
    if (value === null || value === undefined) return true;

    var regex = params['htmlregexpattern'];
    return !value.match(regex);
}, '');

Upvotes: 3

Views: 566

Answers (1)

Alexander Dayan
Alexander Dayan

Reputation: 2924

Most probably, the problem is in this line:

if (value === null || value === undefined) return true;

Try to change it to

if ((value === null) || (value === undefined)) return true;

Exaplanation:

MS minification algorithm removes unnecessary spaces. It 'knows' language keywords like "var' or 'return', but 'null' is not one of them. So, the minified line will be

if(value===null||value===undefined)return true;

Now from JavaScript point of view we have a strange variable named 'null||value'. Enclosing the conditions in the brackets solves the problem:

if(value===null)||(value===undefined)return true;

Upvotes: 4

Related Questions