This is a stupid question, and I am aware that it is; but never the less, here it comes:
Is it possible to close a <script>-tag within itself, so to speak? I mean if you are using an external javascript-document can you close the tag like this:
<script type="text/javascript" src="xxx.js" />
As far as I am aware of it, no.
Browsers usually ignore self closing script tags.
Have a look at this topic
Why don't self-closing script tags work?
If the document is XHTML served as XML, then yes, that's perfectly valid. If you're serving XHTML as HTML, however, it's not recommended as the SGML-style parsers that browsers use to processes tag-soup HTML may have a problem with it. (If you do decide to use self-closing script tags like that, be sure and test in several browsers first!)
Related
I am working on some pretty old code and in every jsp file where there is a javaScript block I see some bizarre Syntax. It goes like :
<script language="JavaScript">
<!--
here is the main code (mixed jsp and javaScript)
//-->
</script>
neccesity <!-- code here //-->
I don't find any necessity for <!-- //--> this syntax. But to remove these from the whole project I really have to be sure why these syntax was used.
Does those had any significance previously, I mean for browser issue or something?
Or,
Just a simple mistake which were carried on.?
HTML style comments in javascript date back to a time when not all browsers supported javascript, so would render the code as text. The <script> tag was just assumed to be just like any other unknown tag, so the tag itself was not rendered, but the contents were
I don't think any current browser would have a problem with it, but I would recommend getting rid of it, because who knows what the future will bring
Does those had any significance previously, I mean for browser issue or something?
Yes, it used to. Remember that a script tag embeds non-HTML inside an HTML document, in this case JavaScript code. Since the content of the script tag isn't HTML, but is being parsed by an HTML parser (while it looks for the ending tag), the idea was to use an HTML comment (<!-- ... -->) to contain the JavaScript code, so the HTML parser wouldn't get confused by HTML-like things within the JavaScript code, and so really old browsers that didn't understand script tags at all wouldn't show the "text" inside them.
The one you've quoted is one of many variations.
There's no need, at all, to do this in an HTML document, with modern browsers — or even with really old browsers, at this point.
In an XHTML document, you do need to do something, but what's shown there is likely to be insufficient; you'd need a CDATA section instead.
Assuming you're writing HTML5 documents (<!doctype html>), you can remove those comments from the JSPs.
As stated here: http://www.w3schools.com/tags/tag_comment.asp
You can also use the comment tag to "hide" scripts from browsers without support for scripts (so they don't show them as plain text)
Nowadays not really an issue I suppose. But may not be the best idea to get rid of them.
Those are comments, just removed if no documentation comments are there
Back in the days there were all sorts of tricks used to make sure that the content looked decent even if the user had javascript or cookies disabled.
This looks like it was meant for hiding the javascript, if the browser didn't understand Javascript.
I just did a little test and if I add a script in the manner illustrated below it seems to execute in most modern major browsers. It executes before the page is loaded, I was wondering if this would work across all browsers (including historic)?
<script type="text/javascript">
alert("hello world");
</script>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
</body>
</html>
I am of course trying to find a way to execute a script to set a page up before any of it is loaded...any input towards this end would be greatly appreciated. Would it be wrong to use this method?
Thanks in advance for any help!
The script gets executed, but the the markup (any element before a DOCTYPE string) puts some browses to quirks mode, which means a large number of poorly documented quirks and oddities, and mess, too.
So whatever your reasons are, you should at least put the element in the first syntactically correct place, namely right after the <head> tag. This can hardly matter as compared with placing it at the start of the document.
Whether the placement solves your real problem is an entirely different thing. You should ask a separate question, clearly describing the problem (rather than an assumed solution), preferably illustrated by some code that demonstrates the problem.
According to the specs,
A conformant document in the HTML syntax must consist of the following
parts, in the following order:
Optionally, a single U+FEFF BYTE ORDER MARK (BOM) character.
Any number of comments and space characters.
A doctype.
Any number of comments and space characters.
An html element, with its attributes (if any) and its contents (if any).
Browsers follow these specs, and your code (even though works now) may break in the future, since
it clearly breaks the rule of order of elements.
Secondly, it's almost always better to load the scripts last for performance gain.
You mention in the comments that you want to hide/show elements before the page is displayed and that onload is too slow. Try using the DOMContentLoaded instead is it triggers as soon as the HTML DOM is built but before all images CSS and other external references is loaded.
That has always worked for me - though I use jQuery's ready event to make it work cross-browser. And it keeps your HTML valid.
I'm seeing malicious Javascript code injected exactly like this, which somehow makes a blank space at the top of a WordPress page. If you click that blank space, you're taken to a site that talks about crypto. The malicious script uses the Javascript atob() function which when then deconverted with base64 and html-escaped causes the crypto page to be loaded.
Just so you know...
as I'm using Cufon's on my website, but also want to make it look good with JavaScript disabled, I decided to use
<script type='text/javascript'>
document.write("<h1 class='naam'><a class='naam' href='mysite.nl'>MyName</a></h1>");
</script>
<noscript>
<h1 class='other_mockup'><a class='naam' href='mysite.nl'>MyName</a></h1>
</noscript>
This works fine. However, when I validate it, I get this error:
document type does not allow element "h1" here
What to do to fix this? :(
See the specification for differences in script and style elements from HTML 4.
You can't use < and > as data in scripts without some form of escape.
This works fine.
Then you are probably serving the document as text/html instead of application/xhtml+xml so browsers are treating it as HTML.
Was looking through my old question and saw that this one still wasn't solved.
If you want to do the same, use #fontface ( CSS ).
To use it, go to this website. It is really easy to install and works better than Cufon's.
<script type="text/javascript" language="JavaScript">
<!--
alert('foo');
//-->
</script>
It's used all over in my company's grails app, but I know < is an illegal javascript character...
Should the <!-- be like //<!-- instead?
No, it is not illegal. From the specification:
The JavaScript engine allows the string "<!--" to occur at the start of a SCRIPT element, and ignores further characters until the end of the line.
It is, however, pointless. It is designed to stop browsers that do not recognise the script element rending the contents as text. The script element has been supported since Netscape 2! This makes the use of that syntax completely pointless today.
If, however, you are using XHTML then it is actively harmful. Since script elements don't contain intrinsic CDATA, markup inside them is treated as markup and not CDATA, so it would actually comment the script out (if the document was processed as XHTML and not tag soup, which would require an application/xhtml+xml content-type).
Should the <!-- be like //<!-- instead?
No, that would (partially) defeat the object.
It's allowed, but it shouldn't be used. It was originally to prevent the JavaScript text showing up in Mosaic FWIR.
It's the old way to make sure JavaScript didn't show up as text if the browser didn't support JavaScript.
See here.
It shouldn't be used.
It hasn't been required since Netscape 1.0
See http://javascript.crockford.com/script.html for what Douglas Crockford has to say about it.
<!-- is a special construct in Javascript, grandfathered in from the days when Javascript was new, and JS-unaware browsers were everywhere. In other words, Javascript understands and ignores the HTML comment opening tag. It's treated as whitespace, and serves only to hide the JS from ancient browsers which don't know Javascript, or don't even know what the <script> tag is.
The html comment closing tag, on the other hand, is NOT a Javascript language construct, and must be commented out with a JS // comment.
I've seen this tag used after javascript functions for over a decade now and never asked WHY. It's seen in most of the tutorials I've seen over this time, but I usually leave it out... it doesn't seem to have an effect one way or another. Can anyone enlighten me as to why this is used?
If it's just to signify the end of a javascript function, wouldn't the right brace be adequate? If it's for a series of functions, the ending script tag is used.
I doubt I need an example, but for all the other readers out there that are also wondering what it's for, here's some code:
function helloWorld() {
document.write('Hello World');
}
//-->
Thanks in advance!
It's a holdover from the days when browsers without JS support were reasonably common. If you were including JS code directly in an HTML file, you'd include HTML comment tags (<!-- -->) around the code so those browsers didn't display it as part of the page. The reason for // --> is so that browsers that do support JS didn't try to interpret the closing HTML comment as part of the JS code.
Back in the days, some browsers did not handle javascript so to avoid errors, you'd put the javascript code inside an HTML comment block "".
Today, the XHTML standards says you should escape your script like
<script type="text/javascript">
<![CDATA[
... unescaped script content ...
]]>
</script>
You don't have to do that for HTML. Refer to:
http://www.w3.org/TR/xhtml1/
http://www.w3schools.com/tags/tag_script.asp
It's to comment out the code, so legacy browsers that don't support JS won't see it.
That's probably pretty much useless nowadays.
Note that it also needs a <!-- in the beginning to make it a comment.
Read this: http://lachy.id.au/log/2005/05/script-comments
If you notice, you can also usually see a <!-- before the scripts.
That syntax is used so browser that don't support Javascript will ignore the body of those scripts.
In fact, in html, anything surrounded by <!-- and --> is considered to be a comment
It's also useful if you're validating your html code, so the js doesn't appear as invalid html markup. As everyone mentioned however, it's fairly obsolete. I personally try never to have inline javascript, always in an external file that can be cached, which makes this style of coding useless
For browsers that do not understand JavaScript or if JavaScript is manually turned off these start <!-- and end html_comment_tags --> will help the bad_data inside the script > / ? < & ^ % # ) ! - ( to be commented out
If a browser understands javascripts it will not parse the last comment_ending tag--> due to the // placed before the html_comment_end tag--> and if the browser does not understand the meaning of // comment tags of javascript(simply because jscript is turned off) then it obviously parses the --> in the usual way and comments out everything inside the <script></script> including the //