Welcome to the new design! Please report any bugs or issues, thanks :)
Web Dev + WordPress + Security

JavaScript Notes Plus

Welcome to Perishable Press! This article covers a plethora of useful JavaScript tips and tricks. For more excellent JavaScript information, check out the JavaScript tag archive. If you like what you see, I encourage you to subscribe to Perishable Press for a periodic dose of online enlightenment ;)

Nifty JavaScript Design Tricks

Clickable divs

Standard design practice dictates that the site logo or banner located at the top of the page links to the home page of the site. There are several methods for including such functionality into a design, including this JavaScript trick that transforms an entire element (e.g., any div) into a link pointing to the site root:

<div id="banner" onclick="location.href='/';" style="cursor:pointer;">
   [ content goes here ]
</div>

Hide/Validate JavaScript Code

JavaScript Comments

Commenting your scripts is an excellent and highly recommended practice. Here are two methods of commenting within a block of JavaScript.

<script type="text/javascript">

// Use two forward slashes for single line comments

/* Use this method for
multi-line comments */

</script>

Link to an External JavaScript File

This is the preferred method of including JavaScript files. It is invisible to non-JavaScript browsers, and keeps the document from breaking during validation. Use if possible.

<script src="external.js" type="text/javascript"></script>

Hiding JavaScript

Hiding JavaScript code from non-compliant browsers is rather easy, just employ the following lines of code:

<script type="text/javascript">
<!--

JavaScript goes here
JavaScript goes here

//-->
</script>

Unfortunately, JavaScript code does not validate as XHTML. Fortunately, it is possible to present your code as invisible to the validators by enveloping it with the following lines of code:

<script type="text/javascript">
<![CDATA[

JavaScript goes here
JavaScript goes here

]]>
</script>
Combining these two methods produces a deadly weapon in the hellish JavaScript battle:
<script type="text/javascript">
<!--//--><![CDATA[//><!--

JavaScript goes here
JavaScript goes here

//--><!]]>
</script>

And, an alternate method has been suggested, but sustains inconsistent consensus:

<script type="text/javascript">
/* <![CDATA[ */

JavaScript goes here
JavaScript goes here

/* ]]> */
</script>

Here is the same commenting method used for CSS:

<style type="text/css">
/* <![CDATA[ */

CSS goes here
CSS goes here

/* ]]> */
</style>

A slightly modified version of the previous method:

<style type="text/css">
/* //<![CDATA[ */
<!--

CSS goes here
CSS goes here

//-->
/* //]]> */
</style>
Here is yet another way to comment out CSS content:
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/

CSS goes here
CSS goes here

/*]]>*/-->
</style>

Finally, although popular, avoiding this method has been recommended:

<script type="text/javascript">
//<![CDATA[
<!--

JavaScript goes here
JavaScript goes here

//-->
//]]>
</script>

Non-JavaScript Browser Support

Use the <noscript> tag to provide support for antiquated browsers and serve 'em some alternate content. The <noscript> tag operates according to the presence/absence of JavaScript. Consider the following:

<noscript>
 <p>This will not be displayed if JavaScript is enabled.</p>
</noscript>

Here is a specific example that would provide a user with the option to continue browsing without JavaScript:

<noscript>
 <p>
  It appears your browser doesn't support JavaScript.
  Please visit <a href="http://domain.tld/noscript.html">the no-script page</a>
  to see a non-JavaScript version of this page.
 </p>
</noscript>

Jeff Starr
About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
USP Pro: Unlimited front-end forms for user-submitted posts and more.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
This new Admin Page Framework looks pretty good. Can't wait to check it out.
Two great places to find awesome plugins: pluginsearch.com and WP's browse new.
2 things I hate to see in stylesheets: _ and #
Love VLC media player but it fails miserably when it comes to randomizing large collections of mp3 and other files.
Dashlane redesigned, stating proudly they "removed all filigree". Should have kept it; the app now looks generic and boring. Killed your identity.
Working on integration for setaPDF + EDD on the new books subdomain. Good times.
Toggle visibility of hidden files on Mac: Cmd + Shift + .