Why js can't understand string '</script>'? [duplicate] - javascript

This question already has answers here:
javascript variable that contains </script>
(3 answers)
Closed 8 years ago.
I have a simple javascript (jsFiddle):
alert('</script>');
Browser fails to understand it.
This is console output:
Uncaught SyntaxError: Unexpected token ILLEGAL
But this script works (jsFiddle):
alert('</scriptt>');//shows alert text '</scriptt>'
Is it some kind of browser bug or normal ECMAScript behaviour?
(browser is Chrome)

Because it is considered as:
<script>
alert('
</script>
');
which is a SyntaxError
You can use
alert( '<\/script>\n');

The HTML parser does not understand JavaScript und thus looks for something which closes the tag which is </script>. If you need '</script>' as string in JavaScript simply use '</s'+'cript>'.
JavaScript isself does not have such a problem, using var x = '</script>'; in nodejs is no problem. The HTML parser is.

This is Javascript embedded in HTML script tags, right?
Then the HTML parser terminates your script in the middle.
Put the Javascript into its own file or break up the string literal. Maybe a CDATA section also works.

Related

Why am I able to run and build Javascript without semicolons? [duplicate]

This question already has answers here:
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
Closed 3 years ago.
In an online course, I'm following on Udemy, the instructor is putting semicolons after lines and says they are needed. I forgot to put some because I'm just getting off of Python and the code still ran.
var firstName = 'John';
console.log(firstName);
var lastName = 'Smith';
var age = 28;
console.log(lastName)
console.log(age)
When I right click on the browser and click on console the first, last name and age are displayed. Can anyone help explain why its working without the semicolons? I'm assuming they are needed when writing Javascript.
This is because Javascript parser will automatically insert semicolon for you in the following situations:
Copypasta from Flavio Copes' Let’s talk about semicolons in JavaScript
when the next line starts with code that breaks the current one (code can spawn on multiple lines)
when the next line starts with a }, closing the current block
when the end of the source code file is reached
when there is a return statement on its own line
when there is a break statement on its own line
when there is a throw statement on its own line
when there is a continue statement on its own line
For best practices, semicolons should be manually inserted.
JavaScript doesn't actually need semicolons. Unless you're writing more than one statement on the same line (please don't, with the exception of for loops)

<script> Invalid comment formatting? [duplicate]

This question already has an answer here:
Does HTML comment <!-- act as a single-line comment in JavaScript, and why?
(1 answer)
Closed 7 years ago.
I have taken on a project that someone else has worked on and I am running into a bunch of script blocks formatted like this:
<script>
<!--
$('...').live('change', function() {
if (...) {
$('...').hide();
$('...').show();
} else {
$('...').show();
$('...').hide();
}
});
//-->
</script>
They are not even the correct comment format for JavaScript.
The code still works but should I worry that this might break in the long term if I start adding to it?
Is this just blatantly wrong and should I remove all the invalid comments?
To formalize my comment.
Back in the day not all browsers supported JavaScript. To make the JS disappear it would be wrapped in an HTML comment and the browser would effectively ignore it.
This hasn't been relevant for some time now and it can be safely deleted.
It can also be left in without harm, but ew.
By way of example, this question from 2009 asks the question re: relevancy, not what it actually is.
This also means I'm old :(

Why does this JavaScript statement begin with a ; [duplicate]

This question already has answers here:
What does the leading semicolon in JavaScript libraries do?
(6 answers)
Closed 9 years ago.
It's looks extraneous, but it must do something.
ref: https://github.com/quirkey/sammy/blob/master/examples/hello_world/index.html
<script type="text/javascript" charset="utf-8">
;(function($) {
//snip
});
$(function() {
//snip
});
})(jQuery);
</script>
This is to make sure previously loaded code that could have not been terminated with a semicolon gets terminated properly, otherwise it would result in an error. You could say it makes the code more tolerant to other people's bugs.
Update: I tested this out and at least in current Chrome and Firefox it makes no difference whether a previous statement is still open, so the semicolon has no effect on that. Idea: that might still be a problem with very old browsers, but I it is just an idea I didn't verify.

difference between $('<div>') and $('<div/>') [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
$(‘<element>’) vs $(‘<element />’) in jQuery
I am used to write $('<div>').
But today I saw a presentation about advanced-jquery by john-resig who use the following syntax $('<div/>').
http://loft.bocoup.com/john-resig-advanced-jquery/
To me they seem to produce the same output.
My question is: is there some difference between
$('<div>') and $('<div/>')?
No, jQuery will normalize those statements into the exact same.
In some earlier version of jQuery tho, it happend to be that <div> was actually faster than <div/> for whatever reason. I don't know yet, if that still applies.
http://jsperf.com/jquery-constructor-performance
Seems like this bug/feature is no longer true.
<div>, <div/>, <div></div>, and even <div/></div> (Yes, this one will only create one element) all trigger the singleTag regular rexpression which makes jQuery to call document.createElement("div"). It was never passed to any html parser at all.
Here's the regex that you can play with, if it returns true, it will be document.createElement'd
var rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/;
<div> is an opening tag. <div/> is a self-closing tag. In this context, however, there is no difference.

Javascript: Funny string concatenation for script tag [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why split the <script> tag when writing it with document.write()?
Browsing through some Chromium source (see line 45ff), I found:
in_script_tag: function(code) {
return "<script>" + code + "</scr" + "ipt>";
},
Does anyone know why the author chose "</scr" + "ipt>" over "</script>"?
Inside a <script> tag </script> ends it, no matter where it occurs.
Example:
<script>
alert('</script>');
// anything here will not be executed anymore
</script>
The reason for this is simply that the browser does not know anything about JavaScript strings etc. so when it sees </script> it always closes the <script> tag.
Because each time HTML parser sees the </script, it automatically considers the javascript fragment finished. )

Categories