dsfjk44656 2015-09-06 09:41
浏览 91

嵌套引号不能用于HTML

I'm trying to use the card validator (Twitter validator) in twitter. I have my code in the variable called content all my other code is working, but the twitter meta tags. I have the quotes of the tags like \" but when I do this the twitter validator doesn't show my card properly.

If I remove the \ then the rest of my code doesn't work properly. Is there a way to escape the quote so that the twitter card validator will accept it and read the meta tags.

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 code. The html is in the content variable.

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();}

My PHP code (I'm dynamically making pages):

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

The error the Twitter validator is giving:

INFO:  Page fetched successfully

INFO:  8 metatags were found

WARN:  Not whitelisted

I know that it says 'Not whitelisted', but my website is white listed (it works on the main page). I'm sure this is being caused due to the nested quotes.

  • 写回答

1条回答 默认 最新

  • dongsimang4036 2015-09-06 10:07
    关注

    You need to URL Encode your query string content so the xmlhttp.open should be:

    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();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示