【问题标题】:Nested quotes not working in HTML嵌套引号在 HTML 中不起作用
【发布时间】:2015-12-02 00:46:24
【问题描述】:

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

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

HTML 代码:

<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 在 content 变量中。

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

我知道它说“未列入白名单”,但我的网站已列入白名单(它在主页上有效)。我确定这是由于嵌套引号引起的。

【问题讨论】:

  • 用单引号代替双引号不是更简单吗? var content = '&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;meta name="twitter:card"...
  • 是的,这确实让它变得更容易了,“\”现在自动添加到代码中。但是,当我将 URL 放入验证器时,它仍然不起作用。推特验证器仍然没有读取它是正确的,因为 \".
  • 不知道你指的是什么验证器,请添加相关代码。
  • 我已经提到我指的是 Twitter 验证器。
  • 所以这可能是cards-dev.twitter.com/validator,你想放什么网址?你可以添加你正在使用的代码吗?

标签: javascript php html twitter escaping


【解决方案1】:

您需要对查询字符串content 进行 URL 编码,因此 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();

【讨论】:

  • 我已经添加了我的 PHP 代码。当我添加这个并在我的 var 内容周围加上普通引号(“...”)时,它不起作用(我的 php 代码不执行)但是当我放单引号时('... ') 它仍然将 \" 添加到所有引号中。
猜你喜欢
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多