<body> <h1>Welcome to my website!</h1> <p>Enter your name:</p> <inputtype="text"id="name"> <buttononclick="greet()">Greet</button> <script> //在input输入框中输入<script>alert('XSS攻击')</script> function greet() { let name = document.getElementById("name").value; document.write("<p>Hello, " + name + "!</p>"); } </script> </body>
<body> <h1>Welcome to my website!</h1> <p>Enter your name:</p> <inputtype="text"id="name"> <buttononclick="greet()">Greet</button> <script> functiongreet() { let name = document.getElementById("name").value; document.write("<p>Hello, " + name + "!</p>"); let script = document.createElement("script"); script.innerHTML = "alert('JavaScript注入xss攻击!')"; document.body.appendChild(script); } </script> </body>
<body> <h1>Welcome to my website!</h1> <p>Enter your name:</p> <inputtype="text"id="name"> <buttononclick="greet()">Greet</button> <scriptsrc="script.js"></script> </body>
</html>
functiongreet() { let name = document.getElementById("name").value; document.write("<p>Hello, " + name + "!</p>"); let url = "http://example.com/search?q=" + encodeURIComponent("<script>alert('XSS攻击')</script>"); window.location.href = url; }