Leaving out type=»text/javascript» language=»javascript» [duplicate]
Most of the script tags I create, I always include type=»text/javascript» language=»javascript» in the tag. My boss however does not. Sometimes he excludes both, sometimes just has language=javascript even without the quotes Now we have not had an issue in any of the major browsers with his tags. I’m talking about all versions of IE, FF, Safari, and Chrome. Personally I feel it’s laziness and just totally improper and bad coding practice to leave stuff out like this even if it works without it. Anyone know if both should be included or just one or is it ok to leave both out in ASP.NET?
I only use type, I’m not aware of any modern specs which define language so it’s redundant. I can’t remember exactly, but I do recall getting into trouble once omitting both type and language (maybe visual studio thought it was vbscript). Also, whether or not you use quotes around any attribute values is a matter of whether you are using a strict xhtml 1 doctype.
6 Answers 6
I suppose this should be updated now that the landscape has changed quite a bit:
For an HTML5 doctype, it’s no longer required. For example: we leave it out of the pages here at Stack Overflow. If you’re using an HTML5 doctype then it’s completely optional and defaults to text/javascript , so you’re absolutely fine leaving it off in every current (or even very old) browser. Realistically, this was also true even in HTML4 though not strictly valid HTML.
For an HTML4 doctype (to be valid), you need it. For a browser to actually function, it’s not strictly needed and will behave just fine (this has been true all the way back to Netscape 2) — but it won’t be valid HTML. If you have an HTML4 doctype, then keep it around and be valid — cause hey, why not?
I would use type=»text/javascript» to be safe in all current browsers, why leave the ambiguity in there to save 21 characters? language=»» however is deprecated, I’d leave it out.
Also, any validator is going to throw an error, though it will likely work inside the browser (unless you’re dealing with something very old).
@Noldorin — I suppose someone disagrees. it’s a shame you aren’t forced to leave a comment when down-voting, if an answer’s wrong it’d be helpful to others to say why, oh well.
@Noldorin: Agreed. So many people downvote without explanation. Such a shame. You get +1 from me, though.
22 chars + space. Agreed on -1 => comment, maybe the author could be let blank for the mere mortals (seen by admins and gods). Discussion for meta
Sorry. a bit of resurrecting the dead here. I know this question is 4 years old now, but I just wanted to note that in HTML 5 the type=»» attribute is no longer required and defaults to «text/javascript»: w3.org/html/wg/drafts/html/master/… Also wanted to note that «text/javascript» is technically obsolete and that «application/javascript» is the modern equivalent: en.wikipedia.org/wiki/Internet_media_type
According to the w3c spec, type is required. So. even though most browsers are going to be robust enough to work without type being properly specified, it is good practice to explicitly set it to text/javascript .
The W3C recommendation for HTML5 says that you do not need to include:
The browser assumes that it is text/javascript unless otherwise stated as a different type.
Douglas Crockford, one of the great authorities and teachers of Javascript, has this to say:
language=»javascript»
This attribute has been deprecated. It was used to select other programming languages and specific versions of JavaScript. You don’t need it. Don’t use it.
type=»text/javascript»
This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.
Your boss may be doing this for the «right» or the «wrong» reasons (i.e. he may be following Crockford’s advice, or he may just be lazy), but I don’t think you can necessarily make a judgement. If the rest of his HTML and JS is sloppy, that’s another thing. I’d venture that the contents of the script tag could be more of a religious thing like tab size or brace placement.
Edit: @coffeeaddict has pointed out that not putting the proper attributes into the tag messes up his compiles. I’d say that trumps any consideration of whether the attributes are strictly correct or necessary, because projects should always build cleanly without errors or warnings. Same goes for validators, etc, if they are part of the project standard.
It’s not about judgement. well maybe it is! It’s sloppy in my mind when developers do not take the time to put in proper attributes. You never know what could change or what one-off issues may happen. Also, as other people have stated it’s annoying as hell for me to see warnings all over my compiler about improper mark-up. It doesn’t make the life of other developers any easier when we leave out stuff that should be in there. We shouldn’t depend on the «assumption» that all browsers are always going to save our asses. And I disagree that religion should override proper mark-up.
One of the instances where Crockford is wrong. The current standard for HTML makes the type attribute mandatory. The drafts for HTML 5 make it optional.
I don’t know if it is lazy; I mean, why type the extra characters if they aren’t necessary and they waste some bandwidth? The html also looks cleaner. Also, we are having to update sites all the time no matter what, to say that MAYBE it will change some day and it really will matter seems mute to me—the web will almost CERTAINLY change in some expected way, which will make you update the site in much more drastic ways than adding in an optional attribute. If this ever broke your site, it would take 15 minutes to refactor and add it in. seems low on the «risk management» totem pole.
If the document is parsed as HTML5, the language will default to JavaScript, and no attribute of any sort is required (for future reference, there is no language attribute in HTML5).
If you’re catering to HTML 4.x or XHTML 1.x, the default scripting language is supposedly determined from the value of the Content-Script-Type header, whether present locally in a META/meta tag (high priority) or as an HTTP header (low priority). The type attribute is still required by HTML 4.x even if the Content-Script-Type header is present (locally or otherwise) since the default scripting language only affects how the values of attributes like onload, onclick, etc. are handled. The type attribute with «text/javascript» as the value ought to be used in the case of JavaScript instead of the language attribute unless you’re catering to old browsers (e.g. IE4, NN4, perhaps IE5/Mac? ; remember that there was a version of IE6 for Windows 98, so the language attribute is definitely outdated enough).
One last bit of information: technically speaking, application/x-javascript is the correct value for JavaScript (unless it became application/javascript without me knowing), but unfortunately text/javascript is the one with the greatest support in terms of cross-browser compatibility.
How To Type Out Text In Js
In JavaScript, you can choose single quotes or double quotes to wrap your strings in. Both of the following will work okay: const sgl = ‘Single quotes.’; const dbl = «Double quotes»; console.log(sgl); console.log(dbl); There is very little difference between the two, and which you use is down to personal preference.
const string = 'The revolution will not be televised.'; console.log(string); const badString1 = This is a test; const badString2 = 'This is a test; const badString3 = This is a test'; const badString = string; console.log(badString); const sgl = 'Single quotes.'; const dbl = "Double quotes"; console.log(sgl); console.log(dbl); const badQuotes = 'What on earth?"; const sglDbl = 'Would you eat a "fish supper"?'; const dblSgl = "I'm feeling blue."; console.log(sglDbl); console.log(dblSgl); const bigmouth = 'I've got no right to take my place…'; const bigmouth = 'I\'ve got no right to take my place…'; console.log(bigmouth); const greeting = `Hello`; const name = 'Chris'; const greeting = `Hello, $`; console.log(greeting); // "Hello, Chris" const one = 'Hello, '; const two = 'how are you?'; const joined = `$$`; console.log(joined); // "Hello, how are you?" const button = document.querySelector('button'); function greet() < const name = prompt('What is your name?'); alert(`Hello $, nice to see you!`); > button.addEventListener('click', greet); const greeting = "Hello"; const name = "Chris"; console.log(greeting + ", " + name); // "Hello, Chris" const greeting = "Hello"; const name = "Chris"; console.log(`$, $`); // "Hello, Chris" const name = "Front "; const number = 242; console.log(`$$`); // "Front 242" const myString = '123'; const myNum = Number(myString); console.log(typeof myNum); const myNum2 = 123; const myString2 = myNum2.toString(); console.log(typeof myString2); const song = 'Fight the Youth'; const score = 9; const highestScore = 10; const output = `I like the song $. I gave it a score of $%.`; console.log(output); // "I like the song Fight the Youth. I gave it a score of 90%." const output = `I like the song. I gave it a score of 90%.`; console.log(output); // I like the song. // I gave it a score of 90%. const output = 'I like the song.\nI gave it a score of 90%.'; console.log(output); // I like the song. // I gave it a score of 90%. const string = 'The revolution will not be televised.';\nconsole.log(string);\n const badString1 = This is a test;\nconst badString2 = 'This is a test;\nconst badString3 = This is a test';\n const badString = string;\nconsole.log(badString);\n const sgl = 'Single quotes.';\nconst dbl = \"Double quotes\";\nconsole.log(sgl);\nconsole.log(dbl);\n const badQuotes = 'What on earth?\";\n const sglDbl = 'Would you eat a \"fish supper\"?';\nconst dblSgl = \"I'm feeling blue.\";\nconsole.log(sglDbl);\nconsole.log(dblSgl);\n const bigmouth = 'I've got no right to take my place…';\n const bigmouth = 'I\\'ve got no right to take my place…';\nconsole.log(bigmouth);\n const greeting = `Hello`;\n const name = 'Chris';\nconst greeting = `Hello, $`;\nconsole.log(greeting); // \"Hello, Chris\"\n const one = 'Hello, ';\nconst two = 'how are you?';\nconst joined = `$$`;\nconsole.log(joined); // \"Hello, how are you?\"\n \n const button = document.querySelector('button');\n\nfunction greet() <\n const name = prompt('What is your name?');\n alert(`Hello $, nice to see you!`);\n>\n\nbutton.addEventListener('click', greet);\n const greeting = \"Hello\";\nconst name = \"Chris\";\nconsole.log(greeting + \", \" + name); // \"Hello, Chris\"\n const greeting = \"Hello\";\nconst name = \"Chris\";\nconsole.log(`$, $`); // \"Hello, Chris\"\n const name = \"Front \";\nconst number = 242;\nconsole.log(`$$`); // \"Front 242\"\n const myString = '123';\nconst myNum = Number(myString);\nconsole.log(typeof myNum);\n const myNum2 = 123;\nconst myString2 = myNum2.toString();\nconsole.log(typeof myString2);\n const song = 'Fight the Youth';\nconst score = 9;\nconst highestScore = 10;\nconst output = `I like the song $. I gave it a score of $%.`;\nconsole.log(output); // \"I like the song Fight the Youth. I gave it a score of 90%.\"\n const output = `I like the song.\nI gave it a score of 90%.`;\nconsole.log(output); // I like the song.\n // I gave it a score of 90%.\n const output = 'I like the song.\\nI gave it a score of 90%.';\nconsole.log(output); // I like the song.\n // I gave it a score of 90%.\n
Javascript Typeout Text Issues
I’m staff on a gaming forum, thus I’m permitted to use a custom html title on my profile and posts. I’ve been playing around with this and have a few issues.
Issue #1: Multiple Instances — Visibility
If I have multiple posts on a single page, that each show the title, the title will only appear in the top post. The rest will be blank.
Issue #2: Multiple Instances — Speed
If I have multple posts on a single page, that each show the title, based on how many instances there are, it will multiply the delay speed of typing and going to the next set of text. I’d be very grateful if anyone could point out the issues they see, I’m fairly new to Javascript.
/* Typeout Text Array */ var items = ["NGU Content Manager", "GTA V Modder", "Progressing Programmer", "Need help? PM me!"]; /* Variable Shit */ var text; arrcount = 0; var delay = 75; var arraydelay = 3000; var cur = 1; var cursor = "_"; var prefix = "> "; /* Typeout Functions */ function sim() < var elem = document.getElementById("kryptus"); elem.value = prefix + text.substr(0, cur) + cursor; cur++ if (cur >text.length) < cur = 1; setTimeout("nextarr()", arraydelay); >else < setTimeout("sim()", delay); >cursorchange(); > function cursorchange() < if (cur == text.length) < cursor = ""; >else < cursor = "_"; >> function nextarr() < if (arrcount >= items.length) < arrcount = 0; >text = items[arrcount]; sim(); arrcount++; > /* Typeout */ var myurl = "http://www.nextgenupdate.com/forums/members/1158931-kryptus.html"; var currenturl = window.location; var centertext = ""; if (POSTBIT_LEGACY && THIS_SCRIPT == 'showthread') < prefix = ""; centertext = "text-align:center;"; >document.write(' '); if (cur == 1) < nextarr(); >else < setTimeout("nextarr()", arraydelay); >>