You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kentico中移除script标签的type属性

解决Kentico 10.0中移除Script标签Type属性的方案

嘿,我来帮你搞定这个W3C警告的问题!在Kentico 10里移除script标签的type="text/javascript"属性,得根据脚本的来源分情况处理,下面是具体步骤:

1. 手动添加的脚本(母版页/页面模板/WebPart)

如果是你自己在母版页、页面模板或者自定义WebPart里写的script标签,直接修改代码就行:

  • 把原来的 <script type="text/javascript" src="/path/to/script.js"></script>
  • 改成 <script src="/path/to/script.js"></script>
    这种情况最直接,改完就能生效。

2. Kentico系统自动生成的核心脚本

Kentico自带的一些核心脚本(比如CMS UI相关的脚本)会自动加上type属性,这时候就得用**响应过滤器(Response Filter)**来统一拦截并修改输出的HTML内容。

你可以创建一个自定义模块来实现这个功能,代码示例如下:

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using CMS;
using CMS.Base;
using CMS.DocumentEngine;

[assembly: RegisterModule(typeof(RemoveScriptTypeAttributeModule))]

public class RemoveScriptTypeAttributeModule : CMSModuleLoader
{
    protected override void OnInit()
    {
        base.OnInit();
        // 注册页面渲染完成事件
        DocumentEvents.Rendered += DocumentEvents_Rendered;
    }

    private void DocumentEvents_Rendered(object sender, DocumentEventArgs e)
    {
        // 替换响应流的过滤器,用于修改输出HTML
        HttpContext.Current.Response.Filter = new RemoveScriptTypeFilter(HttpContext.Current.Response.Filter);
    }
}

public class RemoveScriptTypeFilter : Stream
{
    private readonly Stream _originalStream;
    private readonly MemoryStream _memoryStream = new MemoryStream();

    public RemoveScriptTypeFilter(Stream originalStream)
    {
        _originalStream = originalStream;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        // 先把输出内容写到内存流
        _memoryStream.Write(buffer, offset, count);
    }

    public override void Flush()
    {
        // 把内存流的内容转换成字符串
        var html = Encoding.UTF8.GetString(_memoryStream.ToArray());
        
        // 用正则移除所有script标签的type="text/javascript"属性(兼容单双引号)
        var cleanedHtml = Regex.Replace(html, @"<script\s+type=""text/javascript""(.*?)>", @"<script$1>", RegexOptions.IgnoreCase);
        cleanedHtml = Regex.Replace(cleanedHtml, @"<script\s+type='text/javascript'(.*?)>", @"<script$1>", RegexOptions.IgnoreCase);
        
        // 把处理后的内容写回原响应流
        var outputBytes = Encoding.UTF8.GetBytes(cleanedHtml);
        _originalStream.Write(outputBytes, 0, outputBytes.Length);
        
        // 重置内存流
        _memoryStream.SetLength(0);
    }

    // 实现Stream抽象类的其他必要方法(空实现即可)
    public override bool CanRead => false;
    public override bool CanSeek => false;
    public override bool CanWrite => true;
    public override long Length => throw new NotSupportedException();
    public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
    public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
    public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
    public override void SetLength(long value) => throw new NotSupportedException();
}

部署方法:

  • 如果你的Kentico项目是Web Application类型:把这段代码编译成DLL,放到网站根目录的Bin文件夹里。
  • 如果是Web Site类型:直接把代码文件放到App_Code文件夹里。

部署后,系统会自动加载这个模块,所有输出的HTML里的script标签type属性都会被移除。

3. 第三方控件/WebPart生成的脚本

如果你用了第三方控件或者自定义WebPart生成的script标签,先检查控件的代码或者配置:

  • 如果是开源控件,直接修改控件源码移除type属性;
  • 如果是闭源控件,同样可以用上面的Response Filter方案统一处理,不用单独修改每个控件。

这样处理完之后,再跑W3C验证,那个“type属性不必要”的警告就应该消失啦!

内容的提问来源于stack exchange,提问作者Sanket Bhujbal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:43:19