TheCarver
TheCarver

Reputation: 19723

TinyMCE - Set Editor Content

I am building an auto-save feature for my TinyMCE editor.

With jQuery, I have saved the editor's content to my database and now want to load the content back in to the editor. I am having troubles with the quotes (") in the HTML coming from the DB.

My code:

var content = "<%=content%>" // Classic ASP variable containing HTML from DB
tinyMCE.activeEditor.setContent(content);

Example output:

var content = "<p>Oh yes, from Churchill, the <em><span style="text-decoration: underline;"><strong>dog</strong></span></em>.</p>"
tinyMCE.activeEditor.setContent(content);

In the variable, "content", there are double quotes in the style tag which is causing a JS error. How do I get around this? Do I replace quotes with single quotes or do I use an escape or encode function? Please help.

Upvotes: 1

Views: 3918

Answers (4)

Kul-Tigin
Kul-Tigin

Reputation: 16950

VBScript has escaping functionality like Javascript (also ready if you use JScript as default language in classic asp)
Check out Escape and UnEscape.
Following is an efficient way of doing to append server-side variables to client-side js block.

// escape on server-side, unescape with js
var content = unescape("<%= escape(content) %>");
tinyMCE.activeEditor.setContent(content);

Example output:

var content = unescape("%3Cp%3EOh%20yes%2C%20from%20Churchill%2C%20the%20%3Cem%3E%3Cspan%20style%3D%22text-decoration%3A%20underline%3B%22%3E%3Cstrong%3Edog%3C/strong%3E%3C/span%3E%3C/em%3E.%3C/p%3E");
tinyMCE.activeEditor.setContent(content);

Upvotes: 2

Thariama
Thariama

Reputation: 50832

Additionally to BeaverusIVs first answer i have to add that the line

str = "<span style="color:black">Text "string"</span>";

should look like this

str = '<span style="color:black">Text "string"</span>';

otherwise the string is not valid and an error is thrown.

Upvotes: 0

BeaverusIV
BeaverusIV

Reputation: 1008

str = "<span style="color:black">Text "string"</span>";
tag_pattern = /\<.*\>/m;
matches = str.match(tag_pattern);
//copy matches array and replace quotes, then use matches array to replace into original string

Upvotes: 0

BeaverusIV
BeaverusIV

Reputation: 1008

If it is just HTML (no javascript) it is probably quickest to replace the double quotes with singles.

Upvotes: 0

Related Questions