【问题标题】:CDATA showing up in my HTML page. Why?CDATA 出现在我的 HTML 页面中。为什么?
【发布时间】:2015-06-13 20:19:08
【问题描述】:

我有一个 HTML 页面,但是当我在手机上加载它时,会出现一个以“CDATA”之类的开头的长文本。 我没有在我的页面上添加任何类似的东西。我使用 Gulp 将我的 JavaScript 缩小并连接到一个文件中。我加载了velocity.js 和jQuery 库。 当我重新加载页面几次时,长文本突然消失了。 我怎样才能摆脱这个问题? 我自己编写了所有代码,没有使用任何库,除了上面提到的那些。

非常感谢您的帮助!谢谢!

【问题讨论】:

  • 如果您查看该页面的页面源代码,您会看到CDATA 部分出现在<script> 块中</head> 之后但<body> 之前。尝试在脚本结束后移动</head> 标签。
  • 每个脚本都在 标签中。

标签: javascript jquery html gulp cdata


【解决方案1】:

我在 AWS 实例中的 wordpress 网站中遇到了同样的问题,而我的移动版页面中有 bitnami。这里的问题是 bitnami 默认加载 pagespeed。我解决了通过 ssh 连接到我在 aws 中的服务器的问题,并使用“cd ..”找到了这个文件(您可以通过 SSH 或通过 FTP 编辑这个文件)。这是SSH方式:

通过 ssh 连接到您的服务器。

在以下位置查找“httpd.conf”文件:

/opt/bitnami/apache2/conf/httpd.conf

使用“nano”命令打开文件。

nano /opt/bitnami/apache2/conf/httpd.conf

然后按“ctrl + w”并查找这 2 行:

Include conf/pagespeed.conf
Include conf/pagespeed_libraries.conf

在两行中使用“#”来注释并保存文件:

#Include conf/pagespeed.conf
#Include conf/pagespeed_libraries.conf

然后用命令行重启apache:

sudo /opt/bitnami/ctlscript.sh restart apache

或者使用你自己的重启命令行,就是这样

问题解决了。

希望对大家有所帮助。

【讨论】:

    【解决方案2】:

    我认为问题在于页面速度脚本,在该脚本中我可以看到以下几行:

    <script pagespeed_no_defer="">//<![CDATA[
    (function(){var g=this,h=function(b,d){var a=b.split("."),c=g;a[0]in c||!c.execScript||c.execScript("var "+a[0]);for(var e;a.length&&(e=a.shift());)a.length||void 0===d?c[e]?c=c[e]:c=c[e]={}:c[e]=d};var l=function(b){var d=b.length;if(0<d){for(var a=Array(d),c=0;c<d;c++)a[c]=b[c];return a}return[]};var m=function(b){var d=window;if(d.addEventListener)d.addEventListener("load",b,!1);else if(d.attachEvent)d.attachEvent("onload",b);else{var a=d.onload;d.onload=function(){b.call(this);a&&a.call(this)}}};var n,p=function(b,d,a,c,e){this.f=b;this.h=d;this.i=a;this.c=e;this.e={height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight,width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth};this.g=c;this.b={};this.a=[];this.d={}},q=function(b,d){var a,c,e=d.getAttribute("pagespeed_url_hash");if(a=e&&!(e in b.d))if(0>=d.offsetWidth&&0>=d.offsetHeight)a=!1;else{c=d.getBoundingClientRect();var f=document.body;a=c.top+("pageYOffset"in window?window.pageYOffset:(document.documentElement||f.parentNode||f).scrollTop);c=c.left+("pageXOffset"in window?window.pageXOffset:(document.documentElement||f.parentNode||f).scrollLeft);f=a.toString()+","+c;b.b.hasOwnProperty(f)?a=!1:(b.b[f]=!0,a=a<=b.e.height&&c<=b.e.width)}a&&(b.a.push(e),b.d[e]=!0)};p.prototype.checkImageForCriticality=function(b){b.getBoundingClientRect&&q(this,b)};h("pagespeed.CriticalImages.checkImageForCriticality",function(b){n.checkImageForCriticality(b)});h("pagespeed.CriticalImages.checkCriticalImages",function(){r(n)});var r=function(b){b.b={};for(var d=["IMG","INPUT"],a=[],c=0;c<d.length;++c)a=a.concat(l(document.getElementsByTagName(d[c])));if(0!=a.length&&a[0].getBoundingClientRect){for(c=0;d=a[c];++c)q(b,d);a="oh="+b.i;b.c&&(a+="&n="+b.c);if(d=0!=b.a.length)for(a+="&ci="+encodeURIComponent(b.a[0]),c=1;c<b.a.length;++c){var e=","+encodeURIComponent(b.a[c]);131072>=a.length+e.length&&(a+=e)}b.g&&(e="&rd="+encodeURIComponent(JSON.stringify(s())),131072>=a.length+e.length&&(a+=e),d=!0);t=a;if(d){c=b.f;b=b.h;var f;if(window.XMLHttpRequest)f=new XMLHttpRequest;else if(window.ActiveXObject)try{f=new ActiveXObject("Msxml2.XMLHTTP")}catch(k){try{f=new ActiveXObject("Microsoft.XMLHTTP")}catch(u){}}f&&(f.open("POST",c+(-1==c.indexOf("?")?"?":"&")+"url="+encodeURIComponent(b)),f.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),f.send(a))}}},s=function(){var b={},d=document.getElementsByTagName("IMG");if(0==d.length)return{};var a=d[0];if(!("naturalWidth"in a&&"naturalHeight"in a))return{};for(var c=0;a=d[c];++c){var e=a.getAttribute("pagespeed_url_hash");e&&(!(e in b)&&0<a.width&&0<a.height&&0<a.naturalWidth&&0<a.naturalHeight||e in b&&a.width>=b[e].k&&a.height>=b[e].j)&&(b[e]={rw:a.width,rh:a.height,ow:a.naturalWidth,oh:a.naturalHeight})}return b},t="";h("pagespeed.CriticalImages.getBeaconData",function(){return t});h("pagespeed.CriticalImages.Run",function(b,d,a,c,e,f){var k=new p(b,d,a,e,f);n=k;c&&m(function(){window.setTimeout(function(){r(k)},0)})});})();pagespeed.CriticalImages.Run('/mod_pagespeed_beacon','http://beta.tim-specht.com/sites/Praxis3/','l-uclZP3o3',true,false,'vYDWyUL8cpc');
    //]]></script>
    

    尝试删除这些行

    【讨论】:

    • 它们不在我的编辑器的 HTML 文件中。我不知道他们来自哪里。
    • 我不是页面速度方面的专家,但我是从您的网站上获取的。也许你有一个添加这些行的 nginx 或 apache pagespeed 插件
    • 所以我得看看我的网络服务器的设置,对吧?
    • 这可以解释为什么我只在移动设备上看到这个......在我的电脑上,我总是从本地网络服务器查看网页。
    • 我也尝试使用这些文件:
    猜你喜欢
    • 2012-08-04
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 2018-07-07
    • 2013-02-16
    • 2012-03-30
    • 2011-12-01
    • 2011-08-04
    相关资源
    最近更新 更多