很棒的 HTML 缩小功能,但会导致 Google Adsense 广告出现问题 - 非常重要!
功能完美运行。它使源代码成为单行。但问题出在 Google Adsense 广告上。他们的位置变得混乱。可能是什么原因?
编程语言是 c# asp.net 4.0
这里的函数
protected override void Render(HtmlTextWriter writer)
{
if (this.Request.Headers["X-MicrosoftAjax"] != "Delta=true")
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"<script[^>]*>[\w|\t|\r|\W]*?</script>");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
base.Render(hw);
string html = sb.ToString();
System.Text.RegularExpressions.MatchCollection mymatch = reg.Matches(html);
html = reg.Replace(html, string.Empty);
reg = new System.Text.RegularExpressions.Regex(@"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}|(?=[\r])\s{2,}");
html = reg.Replace(html, string.Empty);
reg = new System.Text.RegularExpressions.Regex(@"</body>");
string str = string.Empty;
foreach (System.Text.RegularExpressions.Match match in mymatch)
{
str += match.ToString();
}
html = reg.Replace(html, str + "</body>");
writer.Write(html);
}
else
base.Render(writer);
}
function works perfectly. it makes source code single line. but problem is about Google Adsense ads. their locations get messed up. what might be the reason ?
programming language is c# asp.net 4.0
here the function
protected override void Render(HtmlTextWriter writer)
{
if (this.Request.Headers["X-MicrosoftAjax"] != "Delta=true")
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"<script[^>]*>[\w|\t|\r|\W]*?</script>");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
base.Render(hw);
string html = sb.ToString();
System.Text.RegularExpressions.MatchCollection mymatch = reg.Matches(html);
html = reg.Replace(html, string.Empty);
reg = new System.Text.RegularExpressions.Regex(@"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}|(?=[\r])\s{2,}");
html = reg.Replace(html, string.Empty);
reg = new System.Text.RegularExpressions.Regex(@"</body>");
string str = string.Empty;
foreach (System.Text.RegularExpressions.Match match in mymatch)
{
str += match.ToString();
}
html = reg.Replace(html, str + "</body>");
writer.Write(html);
}
else
base.Render(writer);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许它会弄乱 AdSense 位置,因为它会移动所有的
Maybe it messes up adsense locations because it moves all the <script> tags to the end of the body.
我认为问题可能在于它使源代码成为一行。
首先,它可以删除重要的空格 - 块元素内的空格很重要但可折叠,因此虽然您可以用单个空格字符替换它们,但无法删除它们。
其次,有时由于浏览器错误,无关紧要的空格被视为重要的空格(尽管这些空格比以前少得多)。
即使没有第二种情况,第一种情况也足够了,没有充分的理由期望代码在像这里这样极端的减少之后仍然可以正常工作。
最后,无论如何,这也没有什么意义。即使文件很大,也不会对渲染时间产生太大影响,因此它只会真正缩短下载时间,但由于源代码中的大多数空白都非常适合 deflate 算法,因此它会无论如何,在您使用 gzip 或 deflate 进行传输后效果不大(假设您正在使用内容编码来压缩;如果不是,为什么要浪费时间来修改源代码,因为这样您可以获得更好的经过尝试和测试的改进?)
I think the problem might be that it makes the source a single line.
First, it could remove significant whitespace - spaces within block elements are significant but collapsable, so while you can replace them all with a single whitespace character, you can't remove them.
Secondly, sometimes insignificant whitespace is treated as significant due to browser bugs (though these are much fewer than they used to be).
Even without the second case, the first suffices that there's no good reason to expect the code to still work correctly after as extreme a reduction as you have here.
Finally, there's not much point in it anyway. Even with a large file it won't have much effect on render time, so it's only really going to improve download time, but since most whitespace in source is quite amenable to the deflate algorithm, it's going to be of little effect after you gzip or deflate it for transmission anyway (assuming you are using content-encoding to compress; if not, why waste time mangling source when you'll get much better tried-and-tested improvements with that?)