Here is a general encode procedure:

var lt = /</g, 
    gt = />/g, 
    ap = /'/g, 
    ic = /"/g;
value = value.toString().replace(lt, "&lt;").replace(gt, "&gt;").replace(ap, "&#39;").replace(ic, "&#34;");

If your user doesn't submit anything to your server you don't even need the above. If the user submits and you are using the user input then the above should be safe. As long as the '<' and '>' are globally sanitized and the parenthesis also are you are good to go.

Answer from Konstantin Dinev on Stack Overflow
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ Security โ€บ Attacks โ€บ XSS
Cross-site scripting (XSS) - Security - MDN Web Docs
An attacker can exploit this to inject an event handler attribute, by using input like some_id onmouseover=alert(1). To prevent the attack, quote the placeholder: ... JavaScript and CSS contexts: inserting input inside <script> or <style> tags is almost always unsafe.
Discussions

javascript - How to avoid "Cross-Site Script Attacks" - Stack Overflow
How do you avoid cross-site script attacks? Cross-site script attacks (or cross-site scripting) is if you for example have a guestbook on your homepage and a client posts some javascript code whic... More on stackoverflow.com
๐ŸŒ stackoverflow.com
jquery - How to prevent cross-site scripting using javascript - Stack Overflow
I want to stop cross-site scripting on a text area. The code below as input data will be vulnerable for the text area: Can anybody help on this to prev... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 22, 2016
xss - How to prevent cross site script attacks using javascript - Stack Overflow
In my application, i have used excel like grid using MVC3. Recently we faced cross site scripting attack issue while testing in the grid by typing 'alert('hack')' in grid cell which is simply an i... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c# - How to prevent XSS (Cross Site Scripting) whilst allowing HTML input - Stack Overflow
You can put javascript in src and href. 2011-08-11T12:33:07.24Z+00:00 ... @Dunhamzzz - That's another rule, concerning tag content. I talked about tags and their attributes, not content. The point is, whereas href/src are useful, onclick is not. 2011-08-11T12:44:01.113Z+00:00 ... Microsoft have produced their own anti-XSS library, Microsoft Anti-Cross Site Scripting ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
PortSwigger
portswigger.net โ€บ web-security โ€บ cross-site-scripting
What is cross-site scripting (XSS) and how to prevent it? | Web Security Academy
... Content security policy (CSP) is a browser mechanism that aims to mitigate the impact of cross-site scripting and some other vulnerabilities. If an application that employs CSP contains XSS-like behavior, then the CSP might hinder or prevent ...
Top answer
1 of 2
14

I'm sure it can be done fx. in PHP by validating forms

Not really. The input stage is entirely the wrong place to be addressing XSS issues.

If the user types, say <script>alert(document.cookie)</script> into an input, there is nothing wrong with that in itself. I just did it in this message, and if StackOverflow didn't allow it we'd have great difficulty talking about JavaScript on the site! In most cases you want to allow any input(*), so that users can use a < character to literally mean a less-than sign.

The thing is, when you write some text into an HTML page, you must escape it correctly for the context it's going into. For PHP, that means using htmlspecialchars() at the output stage:

<p> Hello, <?php echo htmlspecialchars($name); ?>! </p>

[PHP hint: you can define yourself a function with a shorter name to do echo htmlspecialchars, since this is quite a lot of typing to do every time you want to put a variable into some HTML.]

This is necessary regardless of where the text comes from, whether it's from a user-submitted form or not. Whilst user-submitted data is the most dangerous place to forget your HTML-encoding, the point is really that you're taking a string in one format (plain text) and inserting it into a context in another format (HTML). Any time you throw text into a different context, you're going to need an encoding/escaping scheme appropriate to that context.

For example if you insert text into a JavaScript string literal, you would have to escape the quote character, the backslash and newlines. If you insert text into a query component in a URL, you will need to convert most non-alphanumerics into %xx sequences. Every context has its own rules; you have to know which is the right function for each context in your chosen language/framework. You cannot solve these problems by mangling form submissions at the input stageโ€”though many naรฏve PHP programmers try, which is why so many apps mess up your input in corner cases and still aren't secure.

(*: well, almost any. There's a reasonable argument for filtering out the ASCII control characters from submitted text. It's very unlikely that allowing them would do any good. Plus of course you will have application-specific validations that you'll want to do, like making sure an e-mail field looks like an e-mail address or that numbers really are numeric. But this is not something that can be blanket-applied to all input to get you out of trouble.)

2 of 2
9

Cross-site scripting attacks (XSS) happen when a server accepts input from the client and then blindly writes that input back to the page. Most of the protection from these attacks involves escaping the output, so the Javascript turns into plain HTML.

One thing to keep in mind is that it is not only data coming directly from the client that may contain an attack. A Stored XSS attack involves writing malicious JavaScript to a database, whose contents are then queried by the web application. If the database can be written separately from the client, the application may not be able to be sure that the data had been escaped properly. For this reason, the web application should treat ALL data that it writes to the client as if it may contain an attack.

See this link for a thorough resource on how to protect yourself: http://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet

๐ŸŒ
Telerik
telerik.com โ€บ blogs โ€บ how-to-prevent-cross-site-scripting-xss-javascript
How to Prevent Cross-Site Scripting (XSS) in JavaScript
October 13, 2023 - Another way to prevent cross-site scripting is to encode all user input before it is displayed on a page. Encoding involves converting special characters into their corresponding HTML entities, which are not executable by a web browser.
Top answer
1 of 1
2

Cross side scripting attack is much bigger than you think and definetly not limited to code you mentioned <script>alert("Hello")</script>.So strictly confined to your question these type of XSS attacks can be prevented by so many php libraries.I will mention a few below.

  • HTML Purifier
  • PHP AntiXSS
  • xssprotect
  • XSS HTML Filter
  • xss_clean.php filter
  • Updating php version

Lets talk about the most prominant method to prevent XSS attack which is Php AntiXss

PHP Anti-XSS Library developing for prevent the XSS(Cross Site Scripting) vulnerabilities on the web applications. PHP Anti-XSS Library automatically detect the encoding of the data that you want filter and if you wish its encoding your data again. Also there are 3 type of filtering option.

3 Types of filtering options here

  1. Blacklist Filtering
  2. Whitelist Filtering
  3. Graylist Filtering

Usage:

Method 1: Setting the encoding of the data

$data = AntiXSS::setEncoding($data, "UTF-8");

Method 2:

Setting the filter for the data

$data = AntiXSS::setFilter($data, "whitelist", "string");

Popular Usage:

Xssescape is a package used to prevent cross site scripting (xss) attack in cross brower.

Usage :

npm install xssescape

app.js

var xs = require('xssescape')
var htmlStr = "<script> alert(document.cookie); </script>";
   xs.strictEscape(htmlStr);

or

var htmlStr = "<script> alert(document.cookie); </script>";
   xs.unescape(htmlStr);

or

var browserURL = "http://example.com/?<script> document.cookie </script>";
   xs.unSafeUrl('/home');   // eg. you can keep what ever you want as a path name.

   // it will reload/refresh the page  with /home after the default url.
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 18612431 โ€บ how-to-prevent-cross-site-script-attacks-using-javascript
xss - How to prevent cross site script attacks using javascript - Stack Overflow
If a user is typing into your page, then it isn't a cross-site scripting attack. For it to be XSS, the data would have to come from another site (e.g. a link or form with the data that triggered the alert embedded in it). ... If you do have an XSS problem, then you probably shouldn't be trying to defend against it with JavaScript - defend against it at the point you inject the submitted data into the page.
Top answer
1 of 5
8

Peter, I'd like to introduce you to two concepts in security;

Blacklisting - Disallow things you know are bad.

Whitelisting - Allow things you know are good.

While both have their uses, blacklisting is insecure by design.

What you are asking, is in fact blacklisting. If there had to be an alternative to <script> (such as <img src="bad" onerror="hack()"/>), you won't be able to avoid this issue.

Whitelisting, on the other hand, allows you to specify the exact conditions you are allowing.

For example, you would have the following rules:

  • allow only these tags: b, i, u, img
  • allow only these attributes: src, href, style

That is just the theory. In practice, you must parse the HTML accordingly, hence the need of a proper HTML parser.

2 of 5
7

Microsoft have produced their own anti-XSS library, Microsoft Anti-Cross Site Scripting Library V4.0:

The Microsoft Anti-Cross Site Scripting Library V4.0 (AntiXSS V4.0) is an encoding library designed to help developers protect their ASP.NET web-based applications from XSS attacks. It differs from most encoding libraries in that it uses the white-listing technique -- sometimes referred to as the principle of inclusions -- to provide protection against XSS attacks. This approach works by first defining a valid or allowable set of characters, and encodes anything outside this set (invalid characters or potential attacks). The white-listing approach provides several advantages over other encoding schemes. New features in this version of the Microsoft Anti-Cross Site Scripting Library include:- A customizable safe list for HTML and XML encoding- Performance improvements- Support for Medium Trust ASP.NET applications- HTML Named Entity Support- Invalid Unicode detection- Improved Surrogate Character Support for HTML and XML encoding- LDAP Encoding Improvements- application/x-www-form-urlencoded encoding support

It uses a whitelist approach to strip out potential XSS content.

Here are some relevant links related to AntiXSS:

  • Anti-Cross Site Scripting Library
  • Microsoft Anti-Cross Site Scripting Library V4.2 (AntiXSS V4.2)
  • Microsoft Web Protection Library
Top answer
1 of 7
13

Escape quotes, filter out the word javascript, restrict the allowed letters, etc.

You might want to just read through the OWASP guidelines on XSS.

This additional page at OWASP should be useful to, it deals with the encoding issues in our discussions below.

2 of 7
14

Since you want current best practices and the latest answer here is August 2012, I thought I might as well weigh in and update this.

Best practises to prevent any type of XSS attack (persistent, reflected, DOM, whatever).

  1. Strictly validate all input. For example, if you're asking for a UK postcode ensure that only letters, numbers and the space character is allowed. Do this server-side and if validation fails, display a message to the user so that they can correct their input. Do this for all variables outside of your control, including query string, POST data, headers and cookies.
  2. Add yourself some security headers. Namely
  • X-XSS-Protection: 1; mode=block to activate reflective XSS browser protection into blocking mode instead of filtering mode. Blocking mode stops attacks like this. Edit 2021-01-28: This header is now deprecated due to browsers like Chrome discontinuing their inclusion of XSS auditors.
  • X-Content-Type-Options: nosniff to prevent JavaScript being inserted into images and other content types.
  • Content-Security-Policy: with strict script-src and style-src's at least. Do not allow unsafe-inline or unsafe-eval. This is the daddy of headers for killing off XSS.
  1. Follow the rules in the OWASP XSS (Cross Site Scripting) Prevention Cheat Sheet when outputting values, however for rule #3 I'd do the following instead:
  • Use HTML data attributes to output anything dynamic on the page.
  • e.g. <body data-foo="@foo" />
  • Where @foo will output an HTML encoded version of the variable. e.g. " /> would give <body data-foo="&quot; &#x2F;&gt;" />
  • Grab these values out using JQuery or JavaScript: var foo = $("body").data("foo");
  • This way you don't need to worry about any double encoding, unless your JavaScript later inserts as HTML, however things are still simpler as you deal with the encoding there too instead of mixing it all together.
  • Use a function like below to HTML encode if you're using document.write, otherwise you could introduce a vulnerability. Ideally though use textContent or JQuery's text() and attr() functions.

Tackle these in reverse order. Concentrate on #3 as this is the primary mitigation for XSS, #2 tells the browser not to execute anything that slips through and #1 is a good defence-in-depth measure (if special characters can't get in, they can't get out). However, #1 is weaker because not all fields can be strictly validated and it can impair functionality (imagine Stack Exchange without the ability to allow "<script>" as an input).


function escapeHtml(str) {
    return String(str)
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#039;")
        .replace(/\//g, "&#x2F;")
}
๐ŸŒ
Reddit
reddit.com โ€บ r/programming โ€บ stack overflow - how do i leave my site vulnerable to cross site scripting attacks?
r/programming on Reddit: Stack Overflow - How do I leave my site vulnerable to Cross Site Scripting attacks?
October 26, 2010 - This technique is known as Cross ... the URLs they accept. You can use NoScript to help protect yourself from this sort of thing until more browsers get their acts together to prevent it....
Top answer
1 of 2
3

No. The best way to prevent it is to ensure that all the information you output onto the page is appropriately encoded.


Some possible examples of why angle brackets (and other special character blocking) is insufficient:

https://security.stackexchange.com/questions/36629/cross-site-scripting-without-special-chars

2 of 2
2

One of the biggest problems with preventing XSS is that a single webpage has many different encoding contexts, some of which may or may not overlap. There's a reason double-encoding is considered inherently dangerous.

Let's see an example. You prohibit < and >, so I can no longer input a HTML element in your page, right? Well, not quite. For example, if you put the text I loaded into an attribute, it will be interpreted differently:

onload="document.write('&lt;script&gt;window.alert(&quot;Gotcha!&quot;)&lt;/script&gt;')"

There's plenty of such opportunities, and each needs their own variant of correct encoding. Even encoding the input as proper HTML text (e.g. turning < into &lt;) may be a vulnerability if the text is then taken in javascript, and used in something like innerHTML, for example.

The same kind of issue occurs with any kind of URL (img src="javascript:alert('I can't let you do that, Dave')"), or with embedding user input in any kind of script (\x3C). URL is especially dangerous, since it does triple encoding - URL encoding, (X)HTML encoding and possibly JavaScript encoding. I'm not sure if it's even possible to have user input that is safe under those conditions :D

Ideally, you want to limit your area of exposure as much as you can. Do not read from the generated document unless you trust the user (e.g. an admin). Avoid multiple encoding, and always make sure you know exactly where each potentially unsafe encoding goes. In XHTML, you have a great option in CDATA sections, which make encoding potentially dangerous code easy, but that might be interpreted incorrectly by browsers that don't support XHTML correctly. Otherwise, use a proper documented encoding method - in JS, this would be innerText. Of course, you need to make sure that your JS script isn't compromised due to user data.

๐ŸŒ
SentinelOne
sentinelone.com โ€บ cybersecurity-101 โ€บ threat-intelligence โ€บ how-to-prevent-cross-site-scripting-xss-attacks
How to Prevent Cross-Site Scripting (XSS) Attacks?
January 8, 2026 - Injected scripts wonโ€™t be executed even if an attacker manages to discover XSS injection vulnerabilities. To prevent cross-site scripting attacks in jQuery, you can pass user inputs to a jQuery selector.
๐ŸŒ
Bright Security
brightsec.com โ€บ blog โ€บ cross-site-scripting-javascript
Cross Site Scripting in JavaScript: Everything You Need to Know - Bright Security
August 10, 2025 - The best way to prevent cross site scripting attacks is to ensure every input field is validated. You should always replace sensitive characters that may cause disruption with their entities.
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ [askjs] how do you protect against xss?
r/javascript on Reddit: [AskJS] How do you protect against XSS?
July 29, 2019 -

Greetings, I am working on web app where i have back end utilizing spring And front end utilizing JavaScript And jQuery. I need to implement total xss protection on the front end side. How do you handle xss to make your app Safe? Would you recommend any libraries which can be used to filter input And other things related to xss (these libraries should be safe) Or how do you usually defend against xss when you Are developing such application ? Thank you

๐ŸŒ
PortSwigger
portswigger.net โ€บ web-security โ€บ cross-site-scripting โ€บ preventing
How to prevent XSS | Web Security Academy
If you require loading of external resources, ensure you only allow scripts that do not aid an attacker to exploit your site. For example, if you whitelist certain domains then an attacker can load any script from those domains.
๐ŸŒ
Mblogs
mbloging.com โ€บ home โ€บ javascript โ€บ preventing cross-site scripting (xss) in javascript
Preventing Cross-Site Scripting (XSS) in JavaScript | Mbloging
January 17, 2025 - Learn strategies to prevent Cross-Site Scripting (XSS) attacks in JavaScript, including input sanitization, output encoding, and using CSP for better security.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ cross-site-scripting-xss-prevention-techniques
Cross Site Scripting (XSS) Prevention Techniques - GeeksforGeeks
July 15, 2025 - The modern browser allows using of CSP or Content Security Policy Headers. With these headers, one can specify a list of domains only from which JavaScript content can be loaded.
๐ŸŒ
PreEmptive
preemptive.com โ€บ blog โ€บ how-to-prevent-xss-attacks
How to Prevent XSS Attacks - PreEmptive Solutions
January 8, 2026 - Learn how to prevent XSS attacks on your website or application and keep your usersโ€™ data safe from any potential cross-site scripting attempt.