473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I include </script> in a string?

Hello!

I have a JavaScript code like the following.

<script>

var s = "</script>";

....

</script>

As the string contains </script>, the browser thinks JavaScript ends
there.
How can I avoid it?

TIA.

Sam

Jan 4 '06 #1
9 1700
VK

sa********@gmail.com wrote:
Hello!

I have a JavaScript code like the following.

<script>

var s = "</script>";

...

</script>

As the string contains </script>, the browser thinks JavaScript ends
there.
How can I avoid it?

TIA.

Sam


var s = "<\/script>";

Jan 4 '06 #2
> var s = "<\/script>";

Thanks.
I didn't know that "/" should be escaped.

Sam

Jan 5 '06 #3
sa********@gmail.com said the following on 1/4/2006 8:23 PM:
var s = "<\/script>";

Thanks.
I didn't know that "/" should be escaped.


It is not that the / should be escaped. It is the sequence of </ that
needs to be broken up. By escaping the / you break up the sequence that
should declare and end tag so that the UA doesn't see the complete
sequence. But it causes no changes in the text to JS.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 5 '06 #4
Lee said the following on 1/4/2006 9:05 PM:
sa********@gmail.com said:
var s = "<\/script>";


Thanks.
I didn't know that "/" should be escaped.

It's not that it *should* be escaped, simply that you *can*
escape it (as you can any character), and doing so means
that your code no longer contains "</script>".

var s = "</\script>";

should work just as well.


Normally it does. But it is not </script> that the UA is supposed to
look for. It is supposed to look for, and end the script block, at </
and escaping the / breaks up that sequence.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 5 '06 #5

Randy Webb wrote:
Lee said the following on 1/4/2006 9:05 PM:
sa********@gmail.com said:
It's not that it *should* be escaped, simply that you *can*
escape it (as you can any character), and doing so means
that your code no longer contains "</script>".

Normally it does. But it is not </script> that the UA is supposed to
look for. It is supposed to look for, and end the script block, at </
and escaping the / breaks up that sequence.


A related question. I know it's asking for trouble and kids, don't try
this at home, but browser shouldn't parse commented out HTML...
so the ancient deprecated:

<script>
<!--
var x="</script>";
// -->
</script>

should work just fine?
....and if it does, another reason post-decrement considered harmful...
'--' inside <!-- --> toggless commen on and off:

<script>
<!--
var y=2;
y--;
var x="</script>";
y--;
// -->
</script>

Just food for thought, fun for language lawyers.

Jan 5 '06 #6
On 2006-01-05, sa********@gmail.com <sa********@gmail.com> wrote:
var s = "<\/script>";


Thanks.
I didn't know that "/" should be escaped.


yeah, never put / immediately after < in javascript even in string
literals.

"<"+"/script>" would work but "<\/script>" is shorter/faster.

Bye.
Jasen
Jan 5 '06 #7
JRS: In article <11*********************@z14g2000cwz.googlegroups. com>,
dated Wed, 4 Jan 2006 13:38:47 local, seen in news:comp.lang.javascript,
sa********@gmail.com posted :
As the string contains </script>, the browser thinks JavaScript ends
there.
How can I avoid it?


To keep all testers happy, ISTM necessary NEVER to have < / letter
in a string literal. The best in javascript is to use < \ / letter
in all cases, not just for </script>. Or so ISTM.

Another approach is to break the string into two before/after the / and
to concatenate with +. That, using &, is the only way I've found for
VBscript.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 5 '06 #8
"bw****@gmail.com" <bw****@gmail.com> writes:
A related question. I know it's asking for trouble and kids, don't try
this at home, but browser shouldn't parse commented out HTML...
so the ancient deprecated:

<script>
<!--
var x="</script>";
// -->
</script>

should work just fine?
Nope, not by any standard or practical reason.

The sequence "<!--" doesn't start an HTML comment inside a script
element, because the content of the script element is not HTML - and
therefore not parsed by the HTML parser, but by the appropriate script
language parser. Only if the browser doesn't understand the script tag
itself will it be considered an HTML comment, but no browser like that
have been build in almost ten years.

By the standards, the script element begun by <script type="...">
should end at the first occurence of "</". There is no room to wiggle
in the standard. However, any end tag other than </script> would
give an invalid HTML document anyway, so the browsers go into error
recovery mode and ignore the "</" when not followed by "script".
...and if it does, another reason post-decrement considered harmful...
'--' inside <!-- --> toggless commen on and off:


I wouldn't deprecate post/pre decrement for that reason (I actually
like them when used responsibly), but yes, it does make little sense
to use <!-- and //--> to comment out a script if the script includes
the "--" character sequence. But it made little sense to begin with :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 5 '06 #9
VK

Lasse Reichstein Nielsen wrote:
I wouldn't deprecate post/pre decrement for that reason (I actually
like them when used responsibly), but yes, it does make little sense
to use <!-- and //--> to comment out a script if the script includes
the "--" character sequence. But it made little sense to begin with :)


Well, this issue was semi-cleared in the last year "quote-to-death"
discussion about <script> and comments :-)

The trick is that everyone in the scheme acts by <Give to Caesar what
is Caesar's, and to God what is God's> :-)

HTML parser locates <script> </script> fragment and forwards
*everything* what is between > and < right to the script engine. It
doesn't try to validate or anyhow check what a hell it's sending to the
engine.

The engine gets the chunk and before anything else, without trying to
understand what a hell it got, peel it like a banana from the starting
<!-- and the ending //-->
Only then it starts to read that kind of crap it got this time. So you
can put any decrements inside with no fear. Besides is it was shown
that "--" by itself doesn't *officially* end the comment. It just ends
a comment section within the same comment. But again it's irrelevant to
this situation because the interpreter doesn't give a damn what's
inside until the banana is peeled out.

As I've shown yesterday, Firefox (and some other browsers?) are trying
to be finicky about the skin: like Firefox peels <!-- //--> like no
problem but it pretends to be upset if it's <[CDATA[. That is nothing
but fallacy to please some sensitive hearts in W3C. With above
algorithm we could start each script with !@#$ and end up with %^&*
What would not have any practical sense - but no change in algorithm
would be needed.

One just has to stop to try to find a standard in this pre-historic
mechanics. It is because it is, and it's enough as an explanation.
Appendix in our stomach is not used anymore for Him knows how long, but
no one seems to be sleepless to find its "purpose in the modern world".
Neither someone is calling to immediately genetically change the
humanity to free it up from this rudiment. So let the <script><!-- will
not spoil your mood neither. ;-)

Jan 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gandalf | last post by:
Hello. I thought that strlen was only available after adding #include <string> to my c++ program, but apperently not, or the compiler is clever enough to find it by it self, or what? The...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
9
by: Howard | last post by:
Hello I need some help with this. I want to assign the content of a static txt/html file to my string a. <script runat="server" language="C#"> private void Page_Load(object sender,...
2
by: Martin Hvidberg | last post by:
Dear list I have found a declaration like this: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include "ectemp.h"
21
by: hemant.singh | last post by:
Hello all, I am try'g to send window.location.href to the server script who will generate dynamic javascript according to the referral name comg in as param Now bcz <script language="javascript"...
12
by: Iddo | last post by:
Hi, I am having a strange problem... I have an HTML file which has 2 script tags: 1) <script language="javascript" id="ABC" src="ABC.js" /> 2) <script id="general" language="javascript">...
2
by: Jofio | last post by:
I have 3 files, namely: dArray.h dArray.cp TestdArray.cpp Problem is when I compile the 'main' program - TestdArray.cpp - , it (the compiller) produces the following error: 'Unable to open...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
2
by: -Karl | last post by:
Couls someone please advise me on this error. What I am trying to do is be able to convert an XML document into arrays. I read that the subs & functions have to be in <scripttags. Thanks! ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.