<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: javascript code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 07 Sep 2008 01:46:00 GMT</pubDate>
    <description>DZone Snippets: javascript code</description>
    <item>
      <title>Finding the URL presented to the browser</title>
      <link>http://snippets.dzone.com/posts/show/5502</link>
      <description>Finding the URL presented to the browser, instead of the actual location of the page.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;SCRIPT LANGUAGE="JavaScript"&gt;&lt;br /&gt;  &lt;br /&gt;      &lt;!--&lt;br /&gt;      {&lt;br /&gt;       document.write(location.href);&lt;br /&gt;      }&lt;br /&gt;      // --&gt;&lt;br /&gt;&lt;/SCRIPT&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 16 May 2008 11:52:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5502</guid>
      <author>Booma (Alan Coleman)</author>
    </item>
    <item>
      <title>Javascript document text replace</title>
      <link>http://snippets.dzone.com/posts/show/4490</link>
      <description>This will find any instance of a chosen word and  swap it out for another (non case sensitive). It comes courtesy of a moderator on webdeveloper.com&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;var words={&lt;br /&gt;'Bill':'William','Miss':'Mrs'&lt;br /&gt;}&lt;br /&gt;var regs=[];&lt;br /&gt;for(arg in words){regs[regs.length]=new RegExp(arg,'g')}&lt;br /&gt;&lt;br /&gt;window.onload=function replaceText(){&lt;br /&gt;var tags=document.getElementsByTagName('body')[0].getElementsByTagName('*');&lt;br /&gt;var i=0,t;&lt;br /&gt;	while(t=tags[i++]){&lt;br /&gt;		if(t.childNodes[0]){&lt;br /&gt;			var j=0, c;&lt;br /&gt;			while(c=t.childNodes[j++]){&lt;br /&gt;				if(c.nodeType==3){&lt;br /&gt;					var k=0;&lt;br /&gt;					for(arg in words){&lt;br /&gt;						c.nodeValue=c.nodeValue.replace(regs[k],words[arg]);&lt;br /&gt;						k++;&lt;br /&gt;					}&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 04 Sep 2007 14:29:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4490</guid>
      <author>Booma (Alan Coleman)</author>
    </item>
    <item>
      <title>target= not valid in XHTML</title>
      <link>http://snippets.dzone.com/posts/show/4074</link>
      <description>Used for pages with strict doctypes (i.e. no target="_blank"). Automagically adds them back in to links with rel="external"&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function externalLinks() {&lt;br /&gt; if (!document.getElementsByTagName) return;&lt;br /&gt; var anchors = document.getElementsByTagName("a");&lt;br /&gt; for (var i=0; i&lt;anchors.length; i++) {&lt;br /&gt;   var anchor = anchors[i];&lt;br /&gt;   if (anchor.getAttribute("href") &amp;&amp; anchor.getAttribute("rel") == "external") {&lt;br /&gt;     anchor.target = "_blank";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;window.onload = externalLinks;&lt;br /&gt;&lt;br /&gt;//////////////&lt;br /&gt;&lt;br /&gt;&lt;a href="whatever" rel="external" title="whatever"&gt;Whatever&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 30 May 2007 14:16:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4074</guid>
      <author>Booma (Alan Coleman)</author>
    </item>
    <item>
      <title>Form name not valid in strict XHTML</title>
      <link>http://snippets.dzone.com/posts/show/4073</link>
      <description>&lt;form name="search" id="search"... &lt;br /&gt;&lt;br /&gt;'name' is not valid in strict XHTML. So instead use the id&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;form id="search"...&lt;br /&gt;&lt;br /&gt;&lt;a href="#" onclick="document.forms['search'].submit();return false" title="Search"&gt;Search&lt;/a&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 30 May 2007 14:08:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4073</guid>
      <author>Booma (Alan Coleman)</author>
    </item>
    <item>
      <title>DOM Image rollover</title>
      <link>http://snippets.dzone.com/posts/show/4061</link>
      <description>This is by Chris Poole (http://chrispoole.com) and it works really well.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function domRollover() {&lt;br /&gt;if (navigator.userAgent.match(/Opera (\S+)/)) {&lt;br /&gt;var operaVersion = parseInt(navigator.userAgent.match(/Opera (\S+)/)[1]);&lt;br /&gt;}&lt;br /&gt;if (!document.getElementById||operaVersion &lt;7) return;&lt;br /&gt;var imgarr=document.getElementsByTagName('img');&lt;br /&gt;var imgPreload=new Array();&lt;br /&gt;var imgSrc=new Array();&lt;br /&gt;var imgClass=new Array();&lt;br /&gt;for (i=0;i&lt;imgarr.length;i++){&lt;br /&gt;if (imgarr[i].className.indexOf('domroll')!=-1){&lt;br /&gt;imgSrc[i]=imgarr[i].getAttribute('src');&lt;br /&gt;imgClass[i]=imgarr[i].className;&lt;br /&gt;imgPreload[i]=new Image();&lt;br /&gt;if (imgClass[i].match(/domroll (\S+)/)) {&lt;br /&gt;imgPreload[i].src = imgClass[i].match(/domroll (\S+)/)[1]&lt;br /&gt;}&lt;br /&gt;imgarr[i].setAttribute('xsrc', imgSrc[i]);&lt;br /&gt;imgarr[i].onmouseover=function(){&lt;br /&gt;this.setAttribute('src',this.className.match(/domroll (\S+)/)[1])&lt;br /&gt;}&lt;br /&gt;imgarr[i].onmouseout=function(){&lt;br /&gt;this.setAttribute('src',this.getAttribute('xsrc'))&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;domRollover();&lt;br /&gt;&lt;br /&gt;//&lt;br /&gt;&lt;br /&gt;Where your image would look like this:&lt;br /&gt;&lt;br /&gt;&lt;img src="images/b_news.gif" alt="News" class="domroll images/b_news_roll.gif" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 24 May 2007 16:23:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4061</guid>
      <author>Booma (Alan Coleman)</author>
    </item>
    <item>
      <title>Remove file extension from getAttribute('src')</title>
      <link>http://snippets.dzone.com/posts/show/4060</link>
      <description>This should set x to anything before an instance of the string ".gif"&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  x=getAttribute('src')&lt;br /&gt;  x=x.substring(0, x.indexOf('.gif'));&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 24 May 2007 16:20:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4060</guid>
      <author>Booma (Alan Coleman)</author>
    </item>
    <item>
      <title>Formatting large numbers with a javascript function</title>
      <link>http://snippets.dzone.com/posts/show/3366</link>
      <description>This function will format a large number like 23000456 to 23,000,456&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function addCommas(nStr)&lt;br /&gt;{&lt;br /&gt;	nStr += '';&lt;br /&gt;	x = nStr.split('.');&lt;br /&gt;	x1 = x[0];&lt;br /&gt;	x2 = x.length &gt; 1 ? '.' + x[1] : '';&lt;br /&gt;	var rgx = /(\d+)(\d{3})/;&lt;br /&gt;	while (rgx.test(x1)) {&lt;br /&gt;		x1 = x1.replace(rgx, '$1' + ',' + '$2');&lt;br /&gt;	}&lt;br /&gt;	return x1 + x2;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;        example = addCommas(example);&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 25 Jan 2007 19:59:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3366</guid>
      <author>Booma (Alan Coleman)</author>
    </item>
    <item>
      <title>Javascript commands from 'a' tags rather than buttons</title>
      <link>http://snippets.dzone.com/posts/show/3269</link>
      <description>In this instance the relevant form is called 'search_form'. This is specified in the a tag version because it's not an input and as such cannot have submit or reset associated with it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    	&lt;input type="submit" value="Search" onclick="javascript:return text_validate()" /&gt;&lt;br /&gt;   	&lt;input type="reset" value="Reset" /&gt;&lt;br /&gt;    	&lt;input type="button" value="Show All News" onclick="javascript:return showAll();" title="Show all news button." /&gt;&lt;br /&gt;&lt;br /&gt;Could be shown as:&lt;br /&gt;&lt;br /&gt;        &lt;a class="blockbutton" title="Search" href="javascript:document.search_form.submit();" onclick="javascript:return text_validate();"&gt;Search&lt;/a&gt;	&lt;br /&gt;	&lt;a class="blockbutton" title="Reset" href="javascript:document.search_form.reset();"&gt;Reset&lt;/a&gt;&lt;br /&gt;	&lt;a class="blockbutton" title="Show All News" href="javascript:showAll();"&gt;Show All News&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Okay, as pointed out by Blonkm this is bad for usability. A better way of doing things would be like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;  function doSubmit(){    &lt;br /&gt;        document.getElementById("whatever").submit();&lt;br /&gt;        return true;   &lt;br /&gt;     }   &lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="#" onclick="doSubmit();return false;" title="Search"&gt;Link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 10 Jan 2007 22:23:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3269</guid>
      <author>Booma (Alan Coleman)</author>
    </item>
    <item>
      <title>An alternative to the Input Type = "Submit" button</title>
      <link>http://snippets.dzone.com/posts/show/2722</link>
      <description>The standard HTML element for users to submit a form is the &lt;input type="submit"&gt; button. Though it is possible to change the formatting of the standard grey button to a certain degree, you can't utilise rollover states.&lt;br /&gt;&lt;br /&gt;If you want greater control over the design of the submit button, then an alternative is to use an anchor link, with a Javascript function to submit the form. The format is as follows:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;a href="javascript:void(document.[form name attribute].submit())"&gt;[Link text]&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;eg: &lt;a href="javascript:void(document.frmForm.submit())"&gt;Submit this Book Review to us!&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You can then utilise style classes the same as you would for any other anchor link. &lt;br /&gt;&lt;br /&gt;If you need to do some JavaScript form validation, then simply replace the submit call with the name of your JavaScript function to validate the form, and move the submit call to the end of the form validation function code.&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 27 Sep 2006 15:34:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2722</guid>
      <author>Booma (Alan Coleman)</author>
    </item>
  </channel>
</rss>
