在两个页面之间传递带有html特殊字符的转义查询字符串


Passing escaped query string with html special chars between two pages

我试图传递一个包含特殊html字符(例如<)的查询字符串。我必须做

window.location.href = ".."

在另一个页面上,我必须使用PHP检索这个查询字符串。但是当我使用isset()检查它时,它返回false!

例如,当我需要使用如下JS来转义<p>时:

function HtmlEncode(s)
      {
          var el = document.createElement("div");
          el.innerText = el.textContent = s;
          s = el.innerHTML;
          return s;
      }
window.location.href = "http://localhost/test.php?t="+HTMLEncode("<p>");

现在的url是:http://localhost/test.php?t=&lt;p&gt;

当我做echo isset($_GET["t"]);时,结果得到false

甚至当我尝试this is a <p> tag时,我得到的$_GET["t"]等于this is a

有人能告诉我发生了什么事吗?

不要使用HTMLEncode()使用encodeURIComponent()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent