5

I want to put "</script>" in a variable in JavaScript but it will be consider as end tag like below example:

<script>     
content.value = aval + '<script> ma_gallery("' + varimgurl + '");</script>'+ sevom;
</script>

Does anyone know a solution ?

2 Answers 2

17

Use '<\/script>'.

In a string literal, an escaped slash is equivalent to a slash so it makes no difference to the JavaScript parser but it breaks up the end tag syntax so it isn't parsed as an end tag by the HTML parser.

(An alternative is to split it up into two strings and concatenate them together, but that is more typing, harder to read, and (albeit not significantly) less efficient).

Sign up to request clarification or add additional context in comments.

5 Comments

Escaping any of the characters except the left angle bracket < will work
@blgt — If you escape the t you'll have a tab instead of the letter t. Escaping the / is traditional, so it is the sensible choice.
@Quentin Good poin\t, forgot the t
now this will not be considered end tag @Quentin ? need little elaboration please
@EhsanSajjad: Unlike Javascript, the HTML parser ignores escape sequences with backslashes. "\/" is a backslash followed by a slash, not a single slash.
2

Split "</script>" into two strings:

content.value = aval + '<script> ma_gallery("' + varimgurl + '");</scr' + 'ipt>' + sevom;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.