If you just want simple show/hide effects I'd recommend including a Javascript library like
jQuery or
Prototype before your script. These add an extra layer of methods for you to manipulate the DOM easily, and they have in-depth documentation that explains how to use each method for what you want to do.
I'll work through how the stuff you posted originally works, and hopefully answer your queries along the way. The initial onclick attribute has 3 calls within it:
| CODE |
| window.location='#'; |
This redirects the browser to an empty anchor and afaik appears to be useless.
| CODE |
| setBossMissions('<div align=\'center\'><table width=\'98%\'><tr><td class=\'quote_menu_cap\' colspan=\'4\' style=\'text-align:left;\'>Missioni per conto di Seth Briars</td></tr><tr><td width=\'25%\' class=\'quote_menu\'><a href=\'../red-dead-redemption/soluzione-esumazione-e-altri-simpatici-hobby.html\'>L\'esumazione e altri sim...</a></td><td width=\'25%\' class=\'quote_menu\'><a href=\'../red-dead-redemption/soluzione-un-bel-giro-con-gli-amici.html\'>Un bel giro con gli amici</a></td><td width=\'25%\' class=\'quote_menu\'><a href=\'../red-dead-redemption/soluzione-lasciate-che-i-morti-seppelliscano-i-morti.html\'>Lasciate che i morti...</a></td><td width=\'25%\'> </td></tr></table/></div>'); |
This calls the setBossMissions() function and sends it a long string of markup as an argument. The apostrophes within the markup are prefixed with backslashes (called "escaping" the characters) because the entire string's
delimiter is an apostrophe. For example, the following code would cause a syntax error for the Javascript parser, since the string is prematurely ended before the word "center", leaving the parser to analyse the word "center" as if it were some kind of language construct:
| CODE |
| setBossMissions('<div align='center'>'); |
Placing a backslash before the character causes the Javascript parser to consider it a normal character, and not part of the language syntax.
As it happens, since both speech marks (") and apostrophes are valid for HTML attributes
and as Javascript string delimiters, the author could have opted for simply using the opposite without having to escape anything. The following two lines would both be valid:
| CODE |
setBossMissions('<div align="center">'); setBossMissions("<div align='center'>"); |
The final of the 3 calls is:
This tells the Javascript parser to prevent the browser from redirecting to the URL in the "href" tag of the <a> element that had the original "onclick" attribute. Without it, the browser redirects immediately after the code in "onclick" is complete, rendering the work it did pointless. Some scripters will instead opt for setting the "href" attribute to something like "#", since this will also stop the browser redirecting. However, the latter method will result in a hyperlink that does nothing at all with a browser that doesn't support Javascript (which simply ignores the "onclick" attribute). The former method allows a valid "href" attribute to still work as a fallback in a Javascript-free environment, and should be used wherever possible.
The setBossMissions() function:
| CODE |
| div = document.getElementById('BossMissions'); |
This locates the first HTML element it can find with an "id" attribute matching "BossMissions", and assigns an object that can be used to manipulate that element to the variable "div".
| CODE |
| div.innerHTML = str; |
This uses the "div" object created above to add content within the id="BossMissions" div. The content is the variable "str", which is the first argument of the BossMissions() function. The argument passed is the long string of markup with all the escaped apostrophes.