【问题标题】:jQuery crash because of bundle minification in release mode由于发布模式下的捆绑缩小,jQuery 崩溃
【发布时间】:2015-04-19 21:17:21
【问题描述】:

我有 jquery 包:

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") ...

在用户注册页面上,我有登录和注册表单,因此表单输入的名称中有 Register.Login. 前缀。基本上它看起来像:

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

当我以发布模式发布我的应用程序时,我在捆绑文件中收到此错误:

这显然是因为输入名称中的点。如何保存点并解决此问题?我已经尝试过BundleTable.EnableOptimizations = false;,但它没有帮助,我不认为这是合适的解决方案,因为它破坏了捆绑的目的。另请注意,问题仅在 Release 模式下发生。

编辑: 捆绑文件列表包含我自己的一个脚本文件,它包含我的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);
}, '');

【问题讨论】:

  • 在同一个包中是否有自己的(不是 jQuery)脚本文件?
  • 在你开始挖掘之前,你为什么不试试下划线,然后在你的代码中用点替换它。或者尝试其他捆绑工具。
  • @AlexanderDayan,只有一个 - jquery.validate.custom.attributes.js,它包含我的ForbidHtmlAttribude 的客户端验证逻辑。没有像 :input[name=Register.Password] 这样损坏的选择器
  • @Legends,点是由mvc框架html助手放置的:ViewData.TemplateInfo.HtmlFieldPrefix = "Login";@Html.TextBoxFor(m =&gt; m.Email),我展示了已经渲染的html
  • @Legends,无需抱歉,感谢您的帮助。

标签: c# jquery asp.net-mvc bundle release-mode


【解决方案1】:

很可能,问题出在这一行:

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

试着改成

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

解释:

MS 缩小算法会删除不必要的空格。它“知道”诸如“var”或“return”之类的语言关键字,但“null”不是其中之一。因此,缩小的行将是

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

现在从 JavaScript 的角度来看,我们有一个名为“null||value”的奇怪变量。将条件括在括号中可以解决问题:

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

【讨论】:

  • 是的,它确实有帮助......但是如何?
  • 是的,根据定义,JavaScript 对这种表示法应该没有问题,但事实上它有。
猜你喜欢
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 2014-03-11
  • 2018-12-31
  • 2011-10-06
相关资源
最近更新 更多