嵌套引号不能用于HTML

时间:2015-09-06 09:41:22

标签: javascript php html twitter escaping

我正在尝试在twitter中使用卡片验证器(Twitter validator)。我的变量中的代码名为content我的其他代码都在工作,但是twitter元标记。我有像“\”这样的标签的引用但是当我这样做时,twitter验证器没有正确显示我的卡。

如果删除\,那么我的其余代码将无法正常工作。有没有办法逃避报价,以便twitter卡验证器接受它并读取元标记。

HTML code:

<html>
    <body>
        Image link: <input type="text" id="img">
        Content: <input type="text" id="content">
        <button onclick="makePage()">click</button>
        <script src="makePage.js">
        </script>
        <script>
            var img = document.getElementById("img").value;
            var content = document.getElementById("content").value;   
        </script>
    </body>
</html>

Javascript代码。 html位于内容变量中。

function makePage(){
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
        alert("webpage " + xmlhttp.responseText + " was successfully created!");
}
var content = '<html><head></head><body><meta name="twitter:card" content="summary_large_image"><meta name="twitter:site" content="@nytimes"><meta name="twitter:creator" content="@SarahMaslinNir"><meta name="twitter:title" content="Parade of Fans for Houston’s Funeral"><meta name="twitter:description" content="NEWARK - The guest list and parade of limousines with celebrities emerging from them seemed more suited to a red carpet event in Hollywood or New York than than a gritty stretch of Sussex Avenue near the former site of the James M. Baxter Terrace public housing project here."><meta name="twitter:image" content=""></body></html>';
xmlhttp.open("GET","makePage.php?content=" + content,true);
xmlhttp.send();}

我的PHP代码(我正在动态制作页面):

<?php
    $content = $_GET["content"];
    $file = "" . uniqid() . ".html";
    file_put_contents($file, $content);
    echo $file;
?>

Twitter验证者给出的错误:

INFO:  Page fetched successfully

INFO:  8 metatags were found

WARN:  Not whitelisted

我知道它显示“未列入白名单”,但我的网站已列入白名单(可在主页上使用)。我确定这是由嵌套引号引起的。

1 个答案:

答案 0 :(得分:0)

您需要对您的查询字符串xmlhttp.open进行网址编码,以便var content = '<html><head></head><body><meta name="twitter:card" content="summary_large_image"><meta name="twitter:site" content="@nytimes"><meta name="twitter:creator" content="@SarahMaslinNir"><meta name="twitter:title" content="Parade of Fans for Houston’s Funeral"><meta name="twitter:description" content="NEWARK - The guest list and parade of limousines with celebrities emerging from them seemed more suited to a red carpet event in Hollywood or New York than than a gritty stretch of Sussex Avenue near the former site of the James M. Baxter Terrace public housing project here."><meta name="twitter:image" content=""></body></html>'; var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","http://www.michelem.org/ddd.php?content=" + encodeURIComponent(content), true); xmlhttp.send(); 应为:

{{1}}