|
| 1 | +[[crossorigin]] |
| 2 | +== `+crossorigin+` |
| 3 | + |
| 4 | +The `+crossorigin+` option controls how {productname} handles cross-origin resource loading. This option accepts a function that determines the appropriate crossorigin attribute value based on the resource being loaded. |
| 5 | + |
| 6 | +*Type:* `+(url: string, resourceType: 'script' | 'stylesheet') => string+` |
| 7 | + |
| 8 | +*Default value:* |
| 9 | + |
| 10 | +* For {cloudname}: A function that returns `+'anonymous'+` for cloud resources and omits the attribute for other resources. |
| 11 | +* For self-hosted: A function that **omits** the crossorigin attribute for all resources. |
| 12 | + |
| 13 | +*Possible values:* A function that returns one of: |
| 14 | + |
| 15 | +* `+'anonymous'+` - Sets crossorigin="anonymous". |
| 16 | +* `+'use-credentials'+` - Sets crossorigin="use-credentials". |
| 17 | +* `+''+` (empty string) - Omits the crossorigin attribute entirely. |
| 18 | + |
| 19 | +=== Example: Basic configuration |
| 20 | + |
| 21 | +[source,js] |
| 22 | +---- |
| 23 | +tinymce.init({ |
| 24 | + selector: 'textarea', |
| 25 | + crossorigin: (url, resourceType) => { |
| 26 | + // Return 'anonymous' for all resources |
| 27 | + return 'anonymous'; |
| 28 | + } |
| 29 | +}); |
| 30 | +---- |
| 31 | + |
| 32 | +=== Example: Conditional crossorigin attributes |
| 33 | + |
| 34 | +[source,js] |
| 35 | +---- |
| 36 | +tinymce.init({ |
| 37 | + selector: 'textarea', |
| 38 | + crossorigin: (url, resourceType) => { |
| 39 | + if (url.startsWith('https://your-domain.com')) { |
| 40 | + return ''; // No crossorigin for same-origin resources |
| 41 | + } |
| 42 | + return 'anonymous'; // Use anonymous for cross-origin resources |
| 43 | + } |
| 44 | +}); |
| 45 | +---- |
| 46 | + |
| 47 | +[IMPORTANT] |
| 48 | +==== |
| 49 | +When loading {productname} from {cloudname}: |
| 50 | +
|
| 51 | +* The initial script tag must include `+crossorigin="anonymous"+` and `+referrerpolicy="origin"+`. |
| 52 | +* Example: |
| 53 | ++ |
| 54 | +[source,html,subs="attributes+"] |
| 55 | +---- |
| 56 | +<script src="{cdnurl}" referrerpolicy="origin" crossorigin="anonymous"></script> |
| 57 | +---- |
| 58 | +==== |
| 59 | + |
| 60 | +[TIP] |
| 61 | +==== |
| 62 | +* The crossorigin function runs for both scripts and stylesheets loaded by {productname}. |
| 63 | +* Using `+'anonymous'+` sends the Origin header without credentials. |
| 64 | +* Using `+'use-credentials'+` sends the Origin header with credentials (not recommended for most cases). |
| 65 | +* An empty string `''` means no crossorigin attribute will be set, as it omits the attribute entirely for the resource. |
| 66 | +* For stylesheet resources, the `+content_css_cors+` option takes precedence over the `+crossorigin+` function. See: xref:add-css-options.adoc#content_css_cors[content_css_cors] for details about cross-origin stylesheet loading. |
| 67 | +==== |
| 68 | + |
| 69 | +=== Setting a global crossorigin function |
| 70 | + |
| 71 | +The crossorigin function can also be set globally using `+tinymce.overrideDefaults()+`: |
| 72 | + |
| 73 | +[source,js] |
| 74 | +---- |
| 75 | +tinymce.overrideDefaults({ |
| 76 | + ...tinymce.defaultOptions, // Preserve existing defaults |
| 77 | + crossorigin: (url, resourceType) => { |
| 78 | + // Your custom crossorigin logic |
| 79 | + return 'anonymous'; |
| 80 | + } |
| 81 | +}); |
| 82 | +---- |
| 83 | + |
| 84 | +[IMPORTANT] |
| 85 | +==== |
| 86 | +When using {cloudname}, preserving the existing defaults is required. Always include `+...tinymce.defaultOptions+` when using `+overrideDefaults+`. |
| 87 | +==== |
| 88 | + |
| 89 | +For more details on the crossorigin attribute, see: link:https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin[MDN Web Docs - HTML attribute: crossorigin]. |
0 commit comments