<?xml version="1.0" encoding="UTF-8" ?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
			<title>Tagged with "javascript"</title>
			<link>http://www.addedbytes.com/feeds/tag-feed/</link>
			<description></description>
			<language>en</language>
			<copyright>Web Development in Brighton - Added Bytes 2006</copyright>
			<ttl>120</ttl>
			<item>
				<title>Good-Looking Tooltips</title>
				<link>http://www.addedbytes.com/blog/good-looking-tooltips/</link>
				<description><![CDATA[ <p>I have implemented <a href="http://www.robertnyman.com/2006/09/27/glt-good-looking-tooltips/">Good-Looking Tooltips</a>, using the excellent technique by <span class="vcard"><a class="fn url" href="http://www.robertnyman.com/">Robert Nyman</a></span>.</p><br />
<br />
<p>There were a couple of small issues along the way - a bit of code</p> <p>I have implemented <a href="http://www.robertnyman.com/2006/09/27/glt-good-looking-tooltips/">Good-Looking Tooltips</a>, using the excellent technique by <span class="vcard"><a class="fn url" href="http://www.robertnyman.com/">Robert Nyman</a></span>.</p>

<p>There were a couple of small issues along the way - a bit of code I added to the site's JavaScript when I was experimenting with XHTML was causing some trouble, and &amp;lt; and &amp;gt; were being treated as HTML rather than as characters. Robert kindly helped me work out the kinks though, and now it's all up and running. For an example, hover over <span style="font-weight: bold; color: #cc0000;" title="This is an example of the Good-Looking Tooptips in action.">this text</span>.</p> <br><br>]]></description>
				<pubDate>Thu, 28 Sep 2006 14:21:13 +0100</pubDate>
				<guid isPermaLink="false">http://www.addedbytes.com/blog/good-looking-tooltips/</guid>
				<dc:creator>Dave Child</dc:creator>
				<a href="/feeds/tag-feed/?tags=javascript&amp;start=0" class="ditto_tag" rel="tag">javascript</a>,<a href="/feeds/tag-feed/?tags=tooltip&amp;start=0" class="ditto_tag" rel="tag">tooltip</a>
			</item>

			<item>
				<title>&quot;Select All&quot; JavaScript for Forms Posting to an Array</title>
				<link>http://www.addedbytes.com/blog/code/select-all-javascript-for-forms-posting-to-an-array/</link>
				<description><![CDATA[ <p>The problem that led to this snippet of code was that when posting from a form to a PHP script, you may sometimes want to have several fields with the same name and different values. For example, you might want people to be able to tick boxes to indicate which cities they have been to from a list.</p> <p>The problem that led to this snippet of code was that when posting from a form to a PHP script, you may sometimes want to have several fields with the same name and different values. For example, you might want people to be able to tick boxes to indicate which cities they have been to from a list. You would normally add "[]" to the name of the field inputs, like so:</p>

<pre class="html">&lt;input type="checkbox" name="cities[]" value="London"&gt; London<br>&lt;input type="checkbox" name="cities[]" value="Paris"&gt; Paris<br>&lt;input type="checkbox" name="cities[]" value="Berlin"&gt; Berlin<br>&lt;input type="checkbox" name="cities[]" value="Madrid"&gt; Madrid<br>&lt;input type="checkbox" name="cities[]" value="Rome"&gt; Rome</pre>

<p>When the form is received by PHP, whichever items are ticked in the cities list above are accessible in the array $_POST['cities']. This is very handy.</p>

<p>Unfortunately, the addition of square brackets causes trouble with JavaScript, especially with a "Select All" function - which allows you to check all boxes at once by clicking a single one. This script works around that using regular expressions.</p>

<pre class="html">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Checkbox Fun&lt;/title&gt;
&lt;script type="text/javascript"&gt;&lt;!--
 
var formblock;
var forminputs;
 
function prepare() {
  formblock= document.getElementById('form_id');
  forminputs = formblock.getElementsByTagName('input');
}
 
function select_all(name, value) {
  for (i = 0; i &lt; forminputs.length; i++) {
    // regex here to check name attribute
    var regex = new RegExp(name, "i");
    if (regex.test(forminputs[i].getAttribute('name'))) {
      if (value == '1') {
        forminputs[i].checked = true;
      } else {
        forminputs[i].checked = false;
  }
    }
  }
}
 
if (window.addEventListener) {
  window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
  window.attachEvent("onload", prepare)
} else if (document.getElementById) {
  window.onload = prepare;
}
 
//--&gt;&lt;/script&gt;
&lt;/head&gt;
 
&lt;body&gt;
 
&lt;form id="form_id" name="myform" method="get" action="search.php"&gt;
 
  &lt;a href="#" onClick="select_all('area', '1');"&gt;Check All Fruit&lt;/a&gt; | &lt;a href="#" onClick="select_all('area', '0');"&gt;Uncheck All 
Fruit&lt;/a&gt;&lt;br&gt;&lt;br&gt;
 
  &lt;input type="checkbox" name="area[]" value="1" /&gt;Apples&lt;br /&gt;
  &lt;input type="checkbox" name="area[]" value="2" /&gt;Bananas&lt;br /&gt;
  &lt;input type="checkbox" name="area[]" value="3" /&gt;Chickens&lt;br /&gt;
  &lt;input type="checkbox" name="area[]" value="4" /&gt;Stoats
 
  &lt;br&gt;&lt;br&gt;&lt;a href="#" onClick="select_all('location', '1');"&gt;Check All Locations&lt;/a&gt; | &lt;a href="#" onClick="select_all('location', 
'0');"&gt;Uncheck All Locations&lt;/a&gt;&lt;br&gt;&lt;br&gt;
 
  &lt;input type="checkbox" name="location[]" value="1" /&gt;Brighton&lt;br /&gt;
  &lt;input type="checkbox" name="location[]" value="2" /&gt;Hove&lt;br /&gt;
 
&lt;/form&gt;
 
&lt;/body&gt;
&lt;/html&gt;</pre> <br><br>]]></description>
				<pubDate>Thu, 28 Jul 2005 10:05:00 +0100</pubDate>
				<guid isPermaLink="false">http://www.addedbytes.com/blog/code/select-all-javascript-for-forms-posting-to-an-array/</guid>
				<dc:creator>Dave Child</dc:creator>
				<a href="/feeds/tag-feed/?tags=all&amp;start=0" class="ditto_tag" rel="tag">all</a>,<a href="/feeds/tag-feed/?tags=array&amp;start=0" class="ditto_tag" rel="tag">array</a>,<a href="/feeds/tag-feed/?tags=checkboxes&amp;start=0" class="ditto_tag" rel="tag">checkboxes</a>,<a href="/feeds/tag-feed/?tags=form&amp;start=0" class="ditto_tag" rel="tag">form</a>,<a href="/feeds/tag-feed/?tags=forms&amp;start=0" class="ditto_tag" rel="tag">forms</a>,<a href="/feeds/tag-feed/?tags=html&amp;start=0" class="ditto_tag" rel="tag">html</a>,<a href="/feeds/tag-feed/?tags=javascript&amp;start=0" class="ditto_tag" rel="tag">javascript</a>,<a href="/feeds/tag-feed/?tags=php&amp;start=0" class="ditto_tag" rel="tag">php</a>,<a href="/feeds/tag-feed/?tags=select&amp;start=0" class="ditto_tag" rel="tag">select</a>
			</item>

			<item>
				<title>VBScript Date Format Functions</title>
				<link>http://www.addedbytes.com/blog/code/vbscript-date-format-functions/</link>
				<description><![CDATA[ Date formatting in VBScript is not quite as powerful as PHP. This function gives you plenty more ways to format dates and times in VBScript with the minimum of effort. <p>VBScript's date formatting doesn't offer a huge number of options compared to PHP's date() function. While it's relatively easy to just write small snippets of code every time you need to format a date a specific way, life would be much easier if there was a function you could call on time and again to do the work for you.</p>

<p>Below, you will find three date format functions, all of which make some use of Unix timestamps, a concept most ASP developers will not be familiar with. Unix timestamps are defined as the number of seconds since the epoch, 00:00:00 01/01/1970, and are a very sensible way to store dates and times in a database, in my opinion. They are very easy to work with, especially when selecting data from a database, and once you are used to them, they can make life much much simpler.</p>

<p><em>NOTE: These functions were updated on the 15th March 2004 to fix a couple of known bugs. In addition, any scripts using these functions may need to be tweaked to take account of your locale. Each locale will use a slightly different date and time format, so please check yours before using these functions, or simply set your locale to UK. To set your locale to UK, you can use the code below:</em></p>

<pre class="php">&lt;%@LANGUAGE=VBSCRIPT" LCID=2057%&gt;</pre>

<p><em>There is also a [url=http://www.microsoft.com/globaldev/reference/lcid-all.mspx]complete list of valid locales[/url] on the Microsoft website, with the values in the right hand column being the ones you will want.</em></p>

<p><em>Finally, the "monthname" function is VBScript is not present in every version, so may need to be defined manually. Please see comments on this article for an example.</em></p>

<p>The first function, UDate(), will convert a normal date into a Unix timestamp. The second function, unUDate(), simply does the reverse, converting any timestamp into a readable date. The last function takes a date and a format string, and returns a formatted date according to what you put into the formatting string.</p>

<h3>UDate()</h3>

<pre class="php">function UDate(oldDate)
    UDate = DateDiff("s", "01/01/1970 00:00:00", oldDate)
end function</pre>

<p>In practice, UDate is very easy to use. For example, if you want to know the current Unix time (the number of seconds since the epoch), you would use something like the following, which would give intCurrent_Unix_Time a value of 1069916571 at the time of writing.</p>

<pre class="php">intCurrent_Unix_Time = UDate(Now())</pre>

<h3>unUDate()</h3>

<pre class="php">function unUDate(intTimeStamp)
    unUDate = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00")
end function</pre>

<p>unUDate is as easy to use as UDate. Simply feed in an integer, and it will return a matching date. It will treat the integer as the number of seconds between the epoch and the desired date, and will return a properly formatted date for normal use in VBScript accordingly.</p>
	
<h3>formatDate()</h3>

<p>Now here's where it starts to get tricky. formatDate is also quite easy to use, but only once you get the hang of how it works. As you can see from the below, formatDate requires two values to be passed to it. The first, format, is a string telling the function how you would like the date formatted, and the second, intTimeStamp, can be either a Unix timestamp (preferred) or a normal VBScript date.</p>

<p>The format of the string is what we want to make use of to lay out our dates exactly as we would like, and perhaps this is best shown with an example. I would like a date displayed on my site in the following format: "7:11am, Thursday 27th November, 2003". In normal VBScript that would be a pain to format - I'd probably just end up giving up and using the normal VBLongTime and VBLongDate to display something similar. I would prefer complete control though...</p>

<p>The format string for formatDate can include a whole selection of characters to represent certain date or time entities. There's a table of the ones included in this function just below here. The format string needs to include these entities where it wants them to be replaced by the correct time or date value. To get the above formatted date and time, then, I could use the following:</p>

<pre class="php">strDateTime = formatDate(<strong>"%g:%i%a, %l %j%O %F, %Y"</strong>, UDate(Now()))</pre>

<p>That looks a lot harder than it actually is. To tell the function we are aiming to replace a specific entity in the string with a time or date element, we use a symbol made up of a percent sign (%) and a letter. It is a case sensitive function, so be careful you form your format strings correctly. Without further ado, here's a list of the entities you can include in the format string (anything in the string that is not an entity from the list below will still be in the string when it has been processed).</p>

<ul><li><span class="listpad">%A</span> - AM or PM</li>
<li><span class="listpad">%a</span> - am or pm</li>
<li><span class="listpad">%m</span> - Month with leading zeroes (01 - 12)</li>
<li><span class="listpad">%n</span> - Month without leading zeroes (1 - 12)</li>
<li><span class="listpad">%F</span> - Month name (January - December)</li>
<li><span class="listpad">%M</span> - Three letter month name (Jan - Dec)</li>
<li><span class="listpad">$d</span> - Day with leading zeroes (01 - 31)</li>
<li><span class="listpad">%j</span> - Day without leading zeroes (1 - 31)</li>
<li><span class="listpad">%H</span> - Hour with leading zeroes (12 hour)</li>
<li><span class="listpad">%h</span> - Hour with leading zeroes (24 hour)</li>
<li><span class="listpad">%G</span> - Hour without leading zeroes (12 hour)</li>
<li><span class="listpad">%g</span> - Hour without leading zeroes (24 hour)</li>
<li><span class="listpad">%i</span> - Minute with leading zeroes (01 to 60)</li>
<li><span class="listpad">%I</span> - Minute without leading zeroes (1 to 60)</li>
<li><span class="listpad">%s</span> - Second with leading zeroes (01 to 60)</li>
<li><span class="listpad">%S</span> - Second without leading zeroes (1 to 60)</li>
<li><span class="listpad">%L</span> - Number of day of week (1 to 7)</li>
<li><span class="listpad">%l</span> - Name of day of week (Sunday to Saturday)</li>
<li><span class="listpad">%D</span> - Three letter name of day of week (Sun to Sat)</li>
<li><span class="listpad">%O</span> - Ordinal suffix (st, nd rd, th)</li>
<li><span class="listpad">%U</span> - UNIX Timestamp</li>
<li><span class="listpad">%Y</span> - Four digit year (2003)</li>
<li><span class="listpad">%y</span> - Two digit year (03)</li></ul>

<p>You can include any of the above in the format string at any point. I would recommend not using a percentage sign if you can help it (except obviously as part of one of the elements above), and in the same vein, using ordinals anywhere except after a number is slightly foolish.</p>

<p>If you would like to see anything else added to the list, above, please email me and I will see what I can do.</p>

<p>The below is the formatDate function itself. To use, simply copy and paste it into a script or include file and call it as above, with whichever format string you require.</p>

<pre class="php">function formatDate(format, intTimeStamp)
    dim unUDate, A

    ' Test to see if intTimeStamp looks valid. If not, they have passed a normal date
    if not (isnumeric(intTimeStamp)) then
        if isdate(intTimeStamp) then
            intTimeStamp = DateDiff("S", "01/01/1970 00:00:00", intTimeStamp)
        else
            response.write "Date Invalid"
            exit function
        end if
    end if
    
    if (intTimeStamp=0) then 
        unUDate = now()
    else
        unUDate = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00")
    end if

    unUDate = trim(unUDate)

    dim startM : startM = InStr(1, unUDate, "/", vbTextCompare) + 1
    dim startY : startY = InStr(startM, unUDate, "/", vbTextCompare) + 1
    dim startHour : startHour = InStr(startY, unUDate, " ", vbTextCompare) + 1
    dim startMin : startMin = InStr(startHour, unUDate, ":", vbTextCompare) + 1

    dim dateDay : dateDay = mid(unUDate, 1, 2)
    dim dateMonth : dateMonth = mid(unUDate, startM, 2)
    dim dateYear : dateYear = mid(unUDate, startY, 4)
    dim dateHour : dateHour = mid(unUDate, startHour, 2)
    dim dateMinute : dateMinute = mid(unUDate, startMin, 2)
    dim dateSecond : dateSecond = mid(unUDate, InStr(startMin, unUDate, ":", vbTextCompare) + 1, 2)

    format = replace(format, "%Y", right(dateYear, 4))
    format = replace(format, "%y", right(dateYear, 2))
    format = replace(format, "%m", dateMonth)
    format = replace(format, "%n", cint(dateMonth))
    format = replace(format, "%F", monthname(cint(dateMonth)))
    format = replace(format, "%M", left(monthname(cint(dateMonth)), 3))
    format = replace(format, "%d", dateDay)
    format = replace(format, "%j", cint(dateDay))
    format = replace(format, "%h", mid(unUDate, startHour, 2))
    format = replace(format, "%g", cint(mid(unUDate, startHour, 2)))

    if (cint(dateHour) &gt; 12) then
        A = "PM"
    else
        A = "AM"
    end if
    format = replace(format, "%A", A)
    format = replace(format, "%a", lcase(A))

    if (A = "PM") then format = replace(format, "%H", left("0" &amp; dateHour - 12, 2))
    format = replace(format, "%H", dateHour)
    if (A = "PM") then format = replace(format, "%G", left("0" &amp; cint(dateHour) - 12, 2))
    format = replace(format, "%G", cint(dateHour))

    format = replace(format, "%i", dateMinute)
    format = replace(format, "%I", cint(dateMinute))
    format = replace(format, "%s", dateSecond)
    format = replace(format, "%S", cint(dateSecond))
    format = replace(format, "%L", WeekDay(unUDate))
    format = replace(format, "%D", left(WeekDayName(WeekDay(unUDate)), 3))
    format = replace(format, "%l", WeekDayName(WeekDay(unUDate)))
    format = replace(format, "%U", intTimeStamp)
    format = replace(format, "11%O", "11th")
    format = replace(format, "1%O", "1st")
    format = replace(format, "12%O", "12th")
    format = replace(format, "2%O", "2nd")
    format = replace(format, "13%O", "13th")
    format = replace(format, "3%O", "3rd")
    format = replace(format, "%O", "th")

    formatDate = format

end function</pre> <br><br>]]></description>
				<pubDate>Mon, 15 Mar 2004 14:07:00 +0000</pubDate>
				<guid isPermaLink="false">http://www.addedbytes.com/blog/code/vbscript-date-format-functions/</guid>
				<dc:creator>Dave Child</dc:creator>
				<a href="/feeds/tag-feed/?tags=asp&amp;start=0" class="ditto_tag" rel="tag">asp</a>,<a href="/feeds/tag-feed/?tags=date&amp;start=0" class="ditto_tag" rel="tag">date</a>,<a href="/feeds/tag-feed/?tags=development&amp;start=0" class="ditto_tag" rel="tag">development</a>,<a href="/feeds/tag-feed/?tags=javascript&amp;start=0" class="ditto_tag" rel="tag">javascript</a>,<a href="/feeds/tag-feed/?tags=programming&amp;start=0" class="ditto_tag" rel="tag">programming</a>,<a href="/feeds/tag-feed/?tags=reference&amp;start=0" class="ditto_tag" rel="tag">reference</a>,<a href="/feeds/tag-feed/?tags=scripts&amp;start=0" class="ditto_tag" rel="tag">scripts</a>,<a href="/feeds/tag-feed/?tags=time&amp;start=0" class="ditto_tag" rel="tag">time</a>,<a href="/feeds/tag-feed/?tags=unix&amp;start=0" class="ditto_tag" rel="tag">unix</a>,<a href="/feeds/tag-feed/?tags=vb&amp;start=0" class="ditto_tag" rel="tag">vb</a>,<a href="/feeds/tag-feed/?tags=vbscript&amp;start=0" class="ditto_tag" rel="tag">vbscript</a>,<a href="/feeds/tag-feed/?tags=webdesign&amp;start=0" class="ditto_tag" rel="tag">webdesign</a>
			</item>

			<item>
				<title>Minimum Font Size</title>
				<link>http://www.addedbytes.com/blog/code/minimum-font-size/</link>
				<description><![CDATA[ Using relative font sizes is a must for an accessible website, but you run the risk of having incredibly small text. This JavaScript workaround will ensure that the majority of your visitors do not need a microscope to read your content. <p>I think it's entirely fair to say that I <del>don't</del> <ins>didn't</ins> like JavaScript. Too many sites seem to be impossible to use without it, and with an ever growing number of users not JavaScript-enabled, creating a site that needs JavaScript to work is shooting yourself in the foot. What's the point of creating a site if before you start you prevent a quarter of your visitors buying your stock or reading your content?</p>

<p>That said, it does have its uses, and this is one of them. This is a simple, short script that will ensure that if you are using relative font sizes on your site, users with their default font size set very low will have their text resized to something more appropriate.</p>

<p>Of course, this won't work for everybody. It isn't an integral part of the site though - users without JavaScript will not even know that they have missed out on anything.</p>

<p>The way this works is relatively simple. The first stage is to grab the font size being used by the user. It's relative to their browser default font size, whatever fonts you have set, and which actual font they are using.</p>

<p>We achieve this by adding a small div to every page. This can be set to be invisible, and on this site I use the invisible div containing the site's "Skip Navigation" link and a link to the Accessibility Statement.</p>

<code>&lt;div class="invisible" id="xdiv" name="xdiv"&gt;&lt;a href="#content" accesskey="2"&gt;Skip Navigation&lt;/a&gt; &amp;middot; &lt;a href="Accessibility/Accessibility_Statement/" title="Accessibility features of this site." accesskey="0"&gt;Accessibility statement&lt;/a&gt;&lt;/div&gt;</code>

<p>This div is set to be invisible to graphical browsers, which have little use for it's content.</p>

<p>Next, we set up our JavaScript code. The following function can be included as an external .js file, or within your header file, if you use one.</p>

<code>function MinimumFontSize() {
    var XDivElement = document.getElementById('xdiv');
    if (XDivElement.offsetHeight < "16") {
        tags = new Array ('body', 'div', 'a', 'td', 'th', 'p', 'span', 'h1', 'h2', 'h3');
        for (j = 0; j < tags.length; j ++) { 
            var getbody = document.getElementsByTagName(tags[j]).item(0); 
            if (getbody) {
                getbody.style.fontSize = '10pt';
            }
        }
    }
}</code>

<p>In order to customise the function above, you might like to check your numbers. The '16' at the beginning of the script is for this site, and is the value it is because I decided that that should be the minimum height for that div, with it's padding, and font settings. Your 'xdiv' will be different, and you may want to set a different minimum font as well. Fortunately it's a very simple script to modify. If you want to experiment, you can add a line into the code like so (this will give you a popup with the height of your 'xdiv' when you load the page:</p>

<code>function MinimumFontSize() {
    var XDivElement = document.getElementById('xdiv');
    <strong>alert(XDivElement.offsetHeight);</strong>
    if (XDivElement.offsetHeight < "16") {</code>

<p>If it sees that the current font size is too small, it will then resize the fonts to 10pt, a small size but an improvement nonetheless, and of course you can change this if you think it too small. You may also need to add elements to the array of tags. The script cycles through the elements I use on this site for text, but you may use entirely different tags around your data, so may need to add to that list.</p>

<p>And finally, we need to add this last bit of code to our <body> tag, in order to execute the function every time a page is viewed. You can include this any number of other places, but I would recommend having it early in the page, or your users may see the small text before it is resized, rather than the script running invisibly.</p>

<code>onload="MinimumFontSize();"</code>

<p>And there you go, a simple way to set a minimum font size for a site, while keeping your site accessible to both those who wish to resize fonts and those without JavaScript.</p> <br><br>]]></description>
				<pubDate>Sun, 02 Nov 2003 19:17:34 +0000</pubDate>
				<guid isPermaLink="false">http://www.addedbytes.com/blog/code/minimum-font-size/</guid>
				<dc:creator>Dave Child</dc:creator>
				<a href="/feeds/tag-feed/?tags=javascript&amp;start=0" class="ditto_tag" rel="tag">javascript</a>
			</item>
	</channel>
</rss>