<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; }