1,3 → 1,7 |
<!DOCTYPE FAQ [ |
<!-- FIXME: is not used for transformation --> |
<!ENTITY trade "™"><!-- trade mark sign, U+2122 ISOnum --> |
]> |
<FAQ VERSION="32.2" DATE="2010-10-08" revision="$Revision$" id="$Id$"> |
<TITLE>FAQ for comp.lang.javascript</TITLE> |
<CONTENTS> |
156,7 → 160,7 |
<P> |
<URL LINKTEXT="ECMA-262">http://www.ecma-international.org/publications/standards/Ecma-262.htm</URL> |
is the international standard that current language implementations |
(JavaScript&trade;, JScript etc.) are based on. |
(JavaScript™, JScript etc.) are based on. |
</P> |
<P> |
<URL LINKTEXT="ECMA-262">http://www.ecma-international.org/publications/standards/Ecma-262.htm</URL> |
271,7 → 275,7 |
individuals on c.l.js. The reviews of these books are provided: |
</P> |
<LIST TYPE="UL"> |
<LI><EM>&quot;JavaScript: The Definitive Guide,&quot;</EM> 5th Edition, by David Flanagan |
<LI><EM>"JavaScript: The Definitive Guide,"</EM> 5th Edition, by David Flanagan |
<UL> |
<LI>Published: 2006-08</LI> |
<LI>Pages: 1018</LI> |
538,7 → 542,7 |
</P> |
|
<P> |
For an event with an offset from UTC, use <ICODE>YYYY-MM-DDThh:mm:ss&#177;hh:mm</ICODE>. |
For an event with an offset from UTC, use <ICODE>YYYY-MM-DDThh:mm:ss±hh:mm</ICODE>. |
</P> |
<P> |
Never use a local date/time format for a non-local event. Instead, use |
567,7 → 571,7 |
<CONTENT TITLE="How do I format a Date object with javascript?" |
ID="formatDate" NUMID="4_30"> |
<P> |
A local <ICODE>Date</ICODE> object where <ICODE>0 &lt;= year &lt;= 9999</ICODE> can be |
A local <ICODE>Date</ICODE> object where <ICODE>0 <= year <= 9999</ICODE> can be |
formatted to a common ISO 8601 format <ICODE>YYYY-MM-DD</ICODE> with:- |
<CODE> |
/** Formats a Date to YYYY-MM-DD (local time), compatible with both |
577,7 → 581,7 |
*/ |
function formatDate(dateInRange) { |
var year = dateInRange.getFullYear(), |
isInRange = year &gt;= 0 &amp;&amp; year &lt;= 9999, yyyy, mm, dd; |
isInRange = year >= 0 && year <= 9999, yyyy, mm, dd; |
if(!isInRange) { |
throw RangeError("formatDate: year must be 0000-9999"); |
} |
654,7 → 658,7 |
|
function toFixedString(n, digits) { |
var unsigned = toUnsignedString(Math.abs(n), digits); |
return (n &lt; 0 ? "-" : "") + unsigned; |
return (n < 0 ? "-" : "") + unsigned; |
} |
|
function toUnsignedString(m, digits) { |
678,7 → 682,7 |
*/ |
function padLeft(input, size, ch) { |
var s = input + ""; |
while(s.length &lt; size) { |
while(s.length < size) { |
s = ch + s; |
} |
return s; |
687,13 → 691,13 |
|
// Test results |
document.writeln([ |
"numberToFixed(9e-3, 12) =&gt; " + numberToFixed(9e-3, 12), |
"numberToFixed(1.255, 2) =&gt; " + numberToFixed(1.255, 2), |
"numberToFixed(1.355, 2) =&gt; " + numberToFixed(1.355, 2), |
"numberToFixed(0.1255, 3) =&gt; " + numberToFixed(0.1255, 3), |
"numberToFixed(0.07, 2) =&gt; " + numberToFixed(0.07, 2), |
"numberToFixed(0.0000000006, 1) =&gt; " + numberToFixed(0.0000000006, 1), |
"numberToFixed(0.0000000006, 0) =&gt; " + numberToFixed(0.0000000006, 0) |
"numberToFixed(9e-3, 12) => " + numberToFixed(9e-3, 12), |
"numberToFixed(1.255, 2) => " + numberToFixed(1.255, 2), |
"numberToFixed(1.355, 2) => " + numberToFixed(1.355, 2), |
"numberToFixed(0.1255, 3) => " + numberToFixed(0.1255, 3), |
"numberToFixed(0.07, 2) => " + numberToFixed(0.07, 2), |
"numberToFixed(0.0000000006, 1) => " + numberToFixed(0.0000000006, 1), |
"numberToFixed(0.0000000006, 0) => " + numberToFixed(0.0000000006, 0) |
].join("\n")); |
</CODE> |
<MOREINFO> |
791,7 → 795,7 |
<CONTENT TITLE="How do I generate a random integer from 1 to n?" |
ID="randomNumber" NUMID="4_22"> |
<P> |
<ICODE>Math.random()</ICODE> returns a value <ICODE>R</ICODE> such that <ICODE>0 &lt;= R &lt; 1.0</ICODE>; therefore: |
<ICODE>Math.random()</ICODE> returns a value <ICODE>R</ICODE> such that <ICODE>0 <= R < 1.0</ICODE>; therefore: |
<CODE> |
// positive integer expected |
function getRandomNumber(n) { |
937,7 → 941,7 |
var bodyElement = document.body; |
|
//square bracket notation, using an expression |
var bodyElement = document[&quot;bo&quot;+&quot;dy&quot;];</CODE> |
var bodyElement = document["bo"+"dy"];</CODE> |
<MOREINFO> |
<URL>notes/square-brackets/</URL> |
</MOREINFO> |
990,7 → 994,7 |
from its <ICODE>elements</ICODE> collection: |
<CODE> |
var frm = document.forms[0]; |
var control = frm.elements[&quot;elementname&quot;]; |
var control = frm.elements["elementname"]; |
</CODE> |
Once a reference to a control is obtained, its (string) <ICODE>value</ICODE> |
property can be read:- |
1022,8 → 1026,8 |
<CONTENT TITLE="My element is named myselect[], how do I access it?" |
ID="propertyAccess" NUMID="4_25"> |
<P> |
Form controls with any &quot;illegal&quot; characters can be accessed with |
<ICODE>formref.elements[&quot;myselect[]&quot;]</ICODE> - The bracket characters, |
Form controls with any "illegal" characters can be accessed with |
<ICODE>formref.elements["myselect[]"]</ICODE> - The bracket characters, |
amongst others, are illegal in ID attributes and javascript |
identifiers, so you should try to avoid them as browsers may |
handle them incorrectly. |
1034,7 → 1038,7 |
</MOREINFO> |
</P> |
</CONTENT> |
<CONTENT TITLE="Why doesn't the global variable &quot;divId&quot; always refer to the element with id=&quot;divId&quot;?" |
<CONTENT TITLE="Why doesn't the global variable "divId" always refer to the element with id="divId"?" |
ID="globalPollution" NUMID="4_41"> |
<P> |
Microsoft introduced a shortcut that can be used to reference |
1044,14 → 1048,14 |
do so only in "quirks" mode. The best approach is the <ICODE>document.getElementById</ICODE> |
method, which is part of the W3C DOM standard and implemented |
in modern browsers (including IE from version 5.0). So an |
element with <ICODE>id=&quot;foo&quot;</ICODE> can be referenced |
element with <ICODE>id="foo"</ICODE> can be referenced |
with:- |
<CODE> |
var el = document.getElementById(&quot;foo&quot;); |
var el = document.getElementById("foo"); |
</CODE> |
Note: make sure not to use the same <ICODE>id</ICODE> twice in the same document |
and do not give an element a <ICODE>name</ICODE> that matches an <ICODE>id</ICODE> |
of another in the same document or it will trigger bugs in MSIE &lt;= 7 with |
of another in the same document or it will trigger bugs in MSIE <= 7 with |
<ICODE>document.getElementsByName</ICODE> and <ICODE>document.getElementById</ICODE>. |
<MOREINFO> |
<URL>https://developer.mozilla.org/en/Using_Web_Standards_in_your_Web_Pages/Using_the_W3C_DOM#Accessing_Elements_with_the_W3C_DOM |
1065,12 → 1069,12 |
<P> |
Using the non-standard but widely implemented |
<ICODE>innerHTML</ICODE> property: |
<ICODE>&lt;div id=&quot;anID&quot;&gt;Some Content&lt;/div&gt;</ICODE> with script: |
<ICODE><div id="anID">Some Content</div></ICODE> with script: |
<CODE> |
document.getElementById(&quot;anID&quot;).innerHTML = |
&quot;Some &lt;em&gt;new&lt;/em&gt; Content&quot;; |
document.getElementById("anID").innerHTML = |
"Some <em>new</em> Content"; |
</CODE> |
Where <ICODE>&quot;anID&quot;</ICODE> is the (unique on the HTML page) |
Where <ICODE>"anID"</ICODE> is the (unique on the HTML page) |
<ICODE>id</ICODE> attribute value of the element to modify. |
</P> |
<P> |
1088,7 → 1092,7 |
If the new content is only text and does not need to replace existing HTML, |
it is more efficient to modify the <ICODE>data</ICODE> property of a text node. |
<CODE> |
document.getElementById(&quot;anID&quot;).firstChild.data = &quot;Some new Text&quot;; |
document.getElementById("anID").firstChild.data = "Some new Text"; |
</CODE> |
</P> |
<P> |
1122,12 → 1126,12 |
<P> |
Example A: |
<CODE> |
&lt;div id="snurgle"&gt;here&lt;/div&gt; |
&lt;script type="text/javascript"&gt; |
<div id="snurgle">here</div> |
<script type="text/javascript"> |
// Don't forget var. |
var snurgleEl = document.getElementById("snurgle"); |
window.alert(snurgleEl.parentNode); |
&lt;/script&gt; |
</script> |
</CODE> |
</P> |
|
1135,11 → 1139,11 |
Example B: |
<CODE> |
// In the HEAD. |
&lt;script type="text/javascript"&gt; |
<script type="text/javascript"> |
window.onload = function(){ |
var snurgleEl = document.getElementById("snurgle"); |
}; |
&lt;/script&gt; |
</script> |
</CODE> |
</P> |
<LIST TYPE="UL" TITLE="Other problems can include:"> |
1293,8 → 1297,8 |
This is the more "standard" property for getting the height of |
the viewport. It usually "works" in modern browsers in |
<DFN>standards mode</DFN>. Notable exceptions include Safari 2 and |
Opera &lt;= 9.25, both of which return the <ICODE>clientHeight</ICODE> |
of the <ICODE>html</ICODE> <EM>element</EM>. (Oddly, Opera &lt;= 9.25 |
Opera <= 9.25, both of which return the <ICODE>clientHeight</ICODE> |
of the <ICODE>html</ICODE> <EM>element</EM>. (Oddly, Opera <= 9.25 |
in standards mode returns the width of the viewport for |
<ICODE>documentElement.clientWidth</ICODE>). |
</P> |
1347,7 → 1351,7 |
|
<CODE> |
var docEl = document.documentElement, |
IS_BODY_ACTING_ROOT = docEl &amp;&amp; docEl.clientHeight === 0; |
IS_BODY_ACTING_ROOT = docEl && docEl.clientHeight === 0; |
docEl = null; |
</CODE> |
|
1380,7 → 1384,7 |
div = d.createElement('div'); |
div.style.height = "2500px"; |
d.body.insertBefore(div, d.body.firstChild); |
var r = d.documentElement.clientHeight &gt; 2400; |
var r = d.documentElement.clientHeight > 2400; |
d.body.removeChild(div); |
return r; |
} |
1485,8 → 1489,8 |
protocol, and port. |
</P> |
<P> |
Any attempt to access a property in such cases will result in a &quot;Permission |
Denied&quot; error. Signed scripts or trusted ActiveX objects can |
Any attempt to access a property in such cases will result in a "Permission |
Denied" error. Signed scripts or trusted ActiveX objects can |
overcome this in limited situations. |
<MOREINFO> |
<URL>http://msdn.microsoft.com/en-us/library/ms533028%28VS.85%29.aspx</URL> |
1565,7 → 1569,7 |
"We will now charge your credit card." (right). |
</P> |
</CONTENT> |
<CONTENT TITLE="How do I prompt a &quot;Save As&quot; dialog for an accepted mime type?" |
<CONTENT TITLE="How do I prompt a "Save As" dialog for an accepted mime type?" |
ID="fileDownload" NUMID="4_33"> |
<P> |
It is not possible with client-side javascript. |
1590,7 → 1594,7 |
should return true from the event. Also a number of browsers |
require a short delay before setting the status to overcome their |
default behaviour with the statusbar. |
<ICODE>onevent=&quot;setTimeout('window.status=\'Moomin\'',15);&quot;</ICODE> |
<ICODE>onevent="setTimeout('window.status=\'Moomin\'',15);"</ICODE> |
</P> |
<P> |
Many browsers are configured, by default, to disallow scripts from setting |
1617,8 → 1621,8 |
that name and your feature string in the onsubmit handler of the |
FORM. |
<CODE> |
&lt;form action=&quot;&quot; method="post" |
target=&quot;wndname&quot; onsubmit=&quot;window.open('',this.target);return true;&quot;&gt;</CODE> |
<form action="" method="post" |
target="wndname" onsubmit="window.open('',this.target);return true;"></CODE> |
<MOREINFO> |
<URL>http://www.htmlhelp.com/reference/html40/forms/form.html</URL> |
</MOREINFO> |
1685,12 → 1689,12 |
</P> |
<P>JSP Example, using Apache Commons: <ICODE>org.apache.commons.lang.StringEscapeUtils</ICODE>: |
<CODE> |
var jsVar = "&lt;%= StringEscapeUtils.escapeJavaScript(str) %&gt;"; |
var jsVar = "<%= StringEscapeUtils.escapeJavaScript(str) %>"; |
</CODE> |
</P> |
<P>PHP example using <ICODE>addcslashes</ICODE>: |
<CODE> |
var jsVar = "&lt;?php echo addcslashes($str,"\\\'\"\n\r"); ?&gt;"; |
var jsVar = "<?php echo addcslashes($str,"\\\'\"\n\r"); ?>"; |
</CODE> |
<MOREINFO> |
<URL>example/addcslashes.php</URL> |
1736,13 → 1740,13 |
</P> |
<P> |
An image will also |
&quot;swallow&quot; the data sent back by the server, so that they will |
"swallow" the data sent back by the server, so that they will |
not be visible anywhere. |
</P> |
<P> |
<CODE> |
var dummyImage = new Image(); |
dummyImage.src = &quot;scriptURL.asp?param=&quot; + varName; |
dummyImage.src = "scriptURL.asp?param=" + varName; |
</CODE> |
</P> |
<P> |
1783,7 → 1787,7 |
element, such as the current time. For example: |
<ICODE>location.replace(location.href+'?d='+new Date().valueOf())</ICODE> |
If the <ICODE>location.href</ICODE> already contains a query String, use: |
<ICODE>location.replace(location.href+'&amp;d='+new Date().valueOf())</ICODE> |
<ICODE>location.replace(location.href+'&d='+new Date().valueOf())</ICODE> |
<MOREINFO> |
<URL>http://www.mnot.net/cache_docs/</URL> |
<URL>http://docs.sun.com/source/816-6408-10/date.htm</URL> |
1841,17 → 1845,17 |
<DD> |
<EM>Note:</EM> For debugging scripts in IE, the Microsoft Script <EM>Editor</EM> |
is recommended. However, if not available, the <URL LINKTEXT="Microsoft Script Debugger" |
>http://www.microsoft.com/downloads/details.aspx?FamilyId=2F465BE0-94FD-4569-B3C4-DFFDF19CCD99&amp;displaylang=en</URL> may be somewhat helpful. |
>http://www.microsoft.com/downloads/details.aspx?FamilyId=2F465BE0-94FD-4569-B3C4-DFFDF19CCD99&displaylang=en</URL> may be somewhat helpful. |
</DD> |
<DD> |
<URL LINKTEXT="Internet Explorer Developer Toolbar" |
>http://www.microsoft.com/downloads/details.aspx?FamilyID=e59c3964-672d-4511-bb3e-2d5e1db91038&amp;displaylang=en</URL> |
>http://www.microsoft.com/downloads/details.aspx?FamilyID=e59c3964-672d-4511-bb3e-2d5e1db91038&displaylang=en</URL> |
</DD> |
<DD> |
To report errors: Wait until a little yellow |
triangle appears at the left end of the status bar, double click |
on it and, when the error dialog box appears, check the &quot;Always |
show errors&quot; checkbox it contains. |
on it and, when the error dialog box appears, check the "Always |
show errors" checkbox it contains. |
|
Or, <ICODE>Internet Options</ICODE>, <ICODE>Advanced</ICODE>, deselect <ICODE>"Disable Script Debugging"</ICODE>, |
select <ICODE>"Display a notification ..."</ICODE>. |
1858,7 → 1862,7 |
</DD> |
<DT>Firefox</DT> |
<DD> |
<ICODE>Tools &gt; Error console</ICODE> (<ICODE>Ctrl</ICODE> + <ICODE>Shift</ICODE> + <ICODE>j</ICODE>). |
<ICODE>Tools > Error console</ICODE> (<ICODE>Ctrl</ICODE> + <ICODE>Shift</ICODE> + <ICODE>j</ICODE>). |
</DD> |
<DD> |
<URL LINKTEXT="Firebug">http://getfirebug.com/</URL> |
1885,7 → 1889,7 |
</DD> |
<DT>Opera</DT> |
<DD> |
Tools &gt; Advanced &gt; Error console |
Tools > Advanced > Error console |
</DD> |
<DD> |
<URL LINKTEXT="Introduction to Opera Dragonfly" |
1903,13 → 1907,13 |
<DT>Chrome</DT> |
<DD> |
JavaScript Console: click the <ICODE>Page</ICODE> menu icon and select |
<ICODE>Developer &gt; JavaScript Console</ICODE>. From here, you'll be |
<ICODE>Developer > JavaScript Console</ICODE>. From here, you'll be |
able to view errors in the JavaScript execution, and enter |
additional javascript commands to execute. |
</DD> |
<DD> |
JavaScript Debugger: available as <ICODE>Page</ICODE> menu icon &gt; <ICODE>Developer</ICODE> |
&gt; Debug JavaScript, the debugger provides a command prompt from which you |
JavaScript Debugger: available as <ICODE>Page</ICODE> menu icon > <ICODE>Developer</ICODE> |
> Debug JavaScript, the debugger provides a command prompt from which you |
can set breakpoints, backtrace, and more. Type <ICODE>help</ICODE> at the debugger |
command line to get started. |
</DD> |
1939,7 → 1943,7 |
may identify the browser and version. These properties are historically |
inaccurate. Some browsers allow the user to set <ICODE>navigator.userAgent</ICODE> to any value. For |
example, Firefox, (type <ICODE>about:config</ICODE> and search <ICODE>useragent</ICODE> |
or Safari, <ICODE>Develop &gt; User Agent &gt; Other...</ICODE>, IE, via Registry. |
or Safari, <ICODE>Develop > User Agent > Other...</ICODE>, IE, via Registry. |
</P> |
<P> |
Other browsers, such as Opera, provide a list of user agents |
1987,7 → 1991,7 |
In practice you can't. While you could create a suitable |
encryption system with a password in the page, the level of |
support you need to do this means it's always simpler to do it |
server-side. Anything that &quot;protects&quot; a page |
server-side. Anything that "protects" a page |
other than the current one is definitely flawed. |
</P> |
</CONTENT> |
2053,7 → 2057,7 |
</MOREINFO> |
</P> |
</CONTENT> |
<CONTENT TITLE="I have &lt;a href=&quot;javascript:somefunction()&quot;&gt; what ... ?" |
<CONTENT TITLE="I have <a href="javascript:somefunction()"> what ... ?" |
ID="javascriptURI" NUMID="4_24"> |
<P> |
Whatever the rest of your question, this is generally a very bad idea. |
2061,10 → 2065,10 |
current document with the value that is returned from the expression. |
For example: |
<CODE> |
&lt;a href=&quot;javascript:'&amp;lt;h1&amp;gt;' + document.lastModified + '&amp;lt;/h1&amp;gt;'&quot;&gt;lastModified&lt;/a&gt; |
<a href="javascript:'<h1>' + document.lastModified + '</h1>'">lastModified</a> |
</CODE> |
will result in replacing the current document with the value |
returned from <ICODE>document.lastModified</ICODE>, wrapped in an <ICODE>&lt;h1&gt;</ICODE> |
returned from <ICODE>document.lastModified</ICODE>, wrapped in an <ICODE><h1></ICODE> |
tag. |
</P> |
<P> |
2088,7 → 2092,7 |
</P> |
<P> |
Instead, use |
<ICODE>&lt;a href=&quot;something.html&quot; onclick=&quot;somefunction();return false&quot;&gt;</ICODE> |
<ICODE><a href="something.html" onclick="somefunction();return false"></ICODE> |
where <ICODE>something.html</ICODE> is a meaningful alternative. Alternatively, |
attach the <ICODE>click</ICODE> callback using an event registry. |
<MOREINFO> |
2106,18 → 2110,18 |
from the xml source <URL>index.xml</URL> using a the XSLT stylesheet |
<URL>index.xsl</URL><!-- which also checks the links -->. |
</P> |
<CONTENT TITLE="Why do some posts have &lt;FAQENTRY&gt; in them?" ID="FAQENTRY" NUMID="5_1"> |
<CONTENT TITLE="Why do some posts have <FAQENTRY> in them?" ID="FAQENTRY" NUMID="5_1"> |
<P> |
If a poster feels that the question they are answering should be |
covered in the FAQ, placing &lt;FAQENTRY&gt; in the post lets the FAQ |
covered in the FAQ, placing <FAQENTRY> in the post lets the FAQ |
robot collect the messages for easy review and inclusion. A Draft Proposal |
for the FAQ is requested and appreciated. |
</P> |
<P> |
The &lt;FAQENTRY&gt; should not be used in posts except in |
The <FAQENTRY> should not be used in posts except in |
conjunction with a suggestion/proposal for this FAQ. It should |
also not be literally quoted in replies, instead it should be |
partly obscured as, e.g. &lt;FAQ**TRY&gt; or similar. |
partly obscured as, e.g. <FAQ**TRY> or similar. |
</P> |
</CONTENT> |
<CONTENT TITLE="How do I make a suggestion?" NUMID="5_2" ID="makeSuggestion"> |