WayneP
WayneP

Reputation: 139

Script tags in TinyMCE fields are not saving correctly

I am running tinyMCE on my site and I'm trying to insert script tags. I am trying to insert my script tags into the body of the source of the tinyMCE instance.

However, when I do I get problems. When I submit the form including the tinyMCE element It strips the tags.

I have tried the following lines below with still no success.

One thing I have noticed though that if I have to post the script code to the section of the source code of the tinymce instance and it works. However, I need to be able to add it to the <body> of the tinymce instance so I can see previews etc of it.

Below is the script tag I'm trying to upload.

<script type="text/javascript" src="http://resources.32red.com/ad.aspx?pid=237638&bid=3344"></script>

Can anyone shed any light on this?

Thanks

Upvotes: 12

Views: 25764

Answers (4)

bes
bes

Reputation: 101

I ended up with this method hiding from tinymce all inside script tag

protect: [/<script>[\s\S]*?<\/script>/g]

check https://regex101.com/r/cH35d9/1 for regexp:

\s - all space

\S - all not space

*? - repeat to first of </script>

and this is muililine because group of []* ignoring newline borders

Supports tinymce5, tinymce6 - https://www.tiny.cloud/docs/tinymce/6/content-filtering/#protect

Upvotes: 0

Alex Celmer
Alex Celmer

Reputation: 121

What I did to solve the problem was putting the script block inside a tag <code> tag.

<code>
     <script>alert("abc");</script>
</code>

Upvotes: 2

dmullings
dmullings

Reputation: 7200

Try the following:

extended_valid_elements: 'script[language|type|src]'

I see that you've already tried updating the extended_valid_elements property using

'script[language|type]' but you forgot to allow the src attribute for the script tags

so right now the src attribute is being stripped out.

Upvotes: 18

anoldermark
anoldermark

Reputation: 381

There seems to be a bug where if a script tag is wrapped in a div TinyMce dumps the script. (even if you have extended_valid_elements correctly) TinyMCE can be very annoying to use

Upvotes: 6

Related Questions