1
以下のコードを実行してください。文字列リテラルの</script>
タグがエスケープされていないため、明らかに失敗します。JavaScriptでHTML特殊文字をエスケープする方法
<script>
var myquestion = "What is the tag used to mark the end of a Javascript section?";
var myanswer = "It's </script>!";
alert(myanswer.length);
function someMoreCode() {
// ...
}
</script>
私は以下に示すように、それを逃れる行う場合は、文字列変数は、現在のリテラルIt's </script>
が含まれている、いないIt's </script>
:
<script>
var myquestion = "What is the tag used to mark the end of a Javascript section?";
var myanswer = "It's </script>";
alert(myanswer.length);
function someMoreCode() {
// ...
}
</script>
ポップアップボックスではなく、ある予想14の17が表示されます文字列の長さはIt's </script>
です。
文字列を定義して内容をIt's </script>
にするにはどうすればよいですか?
"<"+"/script"
のようなものは適切ではないので、実際の文字列の内容がデータベースに格納されたユーザー提供のデータから来るように、任意の文字列に適用できる汎用メソッドが必要です。
スタッフにそれをまたはhttpsを参照してください/ /stackoverflow.com/questions/236073/why-split-the-script-tag-when-writing-it-with-document-write –
バックスラッシュ '<\/script> 'を使用すると、 – mplungjan