This is my source code:
<!DOCTYPE html>
<html>
<head>
<title>XSS</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script>
var output = "";
<?php
if (isset($_GET['q'])) {
printf('output = "%s";', htmlspecialchars($_GET['q'],ENT_QUOTES, 'UTF-8'));
}
?>
$(function() {
$("#output").html(output);
});
</script>
</head>
<body>
<p id="output"></p>
</body>
</html>
When I send q=\x3cscript\x3ealert(1)\x3c/script\x3e
to my script, the alert is fired. How can I prevent this?