# Bad value X for attribute “src” on element “script”: Illegal character in path segment: space is not allowed.

> Canonical HTML version: https://rocketvalidator.com/html-validation/bad-value-x-for-attribute-src-on-element-script-illegal-character-in-path-segment-space-is-not-allowed
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

URLs follow strict syntax rules defined by RFC 3986, which does not permit literal space characters anywhere in the URI — including path segments, query strings, and fragment identifiers. When the W3C HTML Validator encounters a space in the `src` attribute of a `<script>` element, it flags it as an illegal character because the attribute value is not a valid URL.

While most modern browsers will silently fix this by encoding the space before making the request, relying on this behavior is problematic for several reasons:

- **Standards compliance:** The HTML specification requires that the `src` attribute contain a valid URL. A URL with a literal space is technically malformed.
- **Cross-browser reliability:** Not all user agents, proxies, or CDNs handle malformed URLs the same way. What works in one browser may fail in another context.
- **Interoperability:** Other tools that consume your HTML — such as linters, crawlers, screen readers, and build pipelines — may not be as forgiving as browsers.
- **Copy-paste and linking issues:** Literal spaces in URLs cause problems when users copy links or when URLs appear in plain-text contexts like emails, where the space may break the URL in two.

## How to fix it

You have three options, listed from most recommended to least:

1. **Rename the file or directory** to eliminate spaces entirely (e.g., use hyphens or underscores). This is the cleanest solution.
2. **Percent-encode the space** as `%20` in the `src` attribute value.
3. **Use a build tool or bundler** that generates references with properly encoded or space-free paths automatically.

Avoid using `+` as a space replacement in path segments. The `+` character represents a space only in `application/x-www-form-urlencoded` query strings, not in URL path segments.

## Examples

### ❌ Invalid: space in the path segment

```html
<script src="https://example.com/media assets/app.js"></script>
```

The space between `media` and `assets` makes this an invalid URL.

### ✅ Fixed: percent-encode the space

```html
<script src="https://example.com/media%20assets/app.js"></script>
```

Replacing the space with `%20` produces a valid, standards-compliant URL.

### ✅ Better: rename to avoid spaces entirely

```html
<script src="https://example.com/media-assets/app.js"></script>
```

Using a hyphen (or underscore) instead of a space is the preferred approach. It keeps URLs clean, readable, and free of encoding issues.

### ❌ Invalid: space in a local relative path

This issue isn't limited to absolute URLs. Relative paths trigger the same error:

```html
<script src="js/my script.js"></script>
```

### ✅ Fixed: encode or rename the local file

```html
<script src="js/my%20script.js"></script>
```

Or, better yet:

```html
<script src="js/my-script.js"></script>
```

### Multiple spaces and other special characters

If a URL contains multiple spaces or other special characters, each one must be individually encoded. For example, `{` becomes `%7B` and `}` becomes `%7D`. A quick reference for common characters:

| Character | Encoded form |
|-----------|-------------|
| Space     | `%20`       |
| `[`       | `%5B`       |
| `]`       | `%5D`       |
| `{`       | `%7B`       |
| `}`       | `%7D`       |

```html
<!-- Invalid -->
<script src="libs/my library [v2].js"></script>

<!-- Valid -->
<script src="libs/my%20library%20%5Bv2%5D.js"></script>

<!-- Best: rename the file -->
<script src="libs/my-library-v2.js"></script>
```

Note that this same rule applies to the `src` attribute on other elements like `<img>`, `<iframe>`, `<audio>`, and `<video>`, as well as the `href` attribute on `<a>` and `<link>`. Whenever you reference a URL in HTML, make sure it contains no literal spaces.
