<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Xavi Esteve &#187; javascript</title>
	<atom:link href="http://xaviesteve.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://xaviesteve.com</link>
	<description>I&#039;m a front end web developer and graphic designer from Barcelona based in London. I am passionate in JavaScript, jQuery, CSS, PHP, Wordpress, usability and SEO.</description>
	<lastBuildDate>Mon, 21 May 2012 11:14:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Simplest Modal Window (less than 1KB) &#8211; jQuery Plugin</title>
		<link>http://xaviesteve.com/3054/simplest-modal-window-less-than-1kb-jquery-plugin/</link>
		<comments>http://xaviesteve.com/3054/simplest-modal-window-less-than-1kb-jquery-plugin/#comments</comments>
		<pubDate>Fri, 16 Mar 2012 15:18:19 +0000</pubDate>
		<dc:creator>Xavi</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[creativecommons]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://xaviesteve.com/?p=3054</guid>
		<description><![CDATA[Following my jQuery minimal plugin series I&#8217;ve now built a very functional and minimal jQuery plugin to create modal windows on the go, it weights exactly 572 Bytes (0.6KB). The modal windows have a slick design using a white rounded border square with a fine shadow: The CSS style is fully customizable, only 8 properties used [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-3072" title="" src="http://xaviesteve.com/wp-content/uploads/2012/03/9.jpeg" alt="" width="600" height="200" /></p>
<p>Following my jQuery minimal plugin series I&#8217;ve now built a very functional and minimal jQuery plugin to create modal windows on the go, it weights exactly 572 Bytes (0.6KB). The modal windows have a slick design using a white rounded border square with a fine shadow:</p>
<p><img class="aligncenter size-full wp-image-3057" title="Simple jQuery Modal Window Plugin" src="http://xaviesteve.com/wp-content/uploads/2012/03/Screen-shot-2012-03-04-at-02.00.29.png" alt="Simple jQuery Modal Window Plugin" width="526" height="147" /></p>
<p>The CSS style is fully customizable, only 8 properties used by default and you can modify most of them to customise it your way.</p>
<p>The script is unobtrusive and has a high-performance, meaning it only loads and modifies the DOM when required and it unloads everything completely when the modal window is closed. Closing the modal window can be done by either clicking outside of the window or by clicking any <code>.mw_close</code> element in the window content.</p>
<h3>Download Simple Modal Window jQuery Plugin</h3>
<p>Copy and paste this code below. Yes, ALL the plugin is in this small piece of code, you should just add it into a JavaScript file you have or directly into the page (inside the jQuery wrapper).</p>
<pre>/* Simplest Modal Window by Xavi Esteve (Creative Commons BY-SA) http://xaviesteve.com/?p=3054 */function mw_close(){$("#mw_win,#mw_mask,#mw_style").remove()}function mw_load(a){mw_mask="background:#333;height:100%;opacity:0.33;position:fixed;top:0;left:0;width:100%;z-index:1000;";mw_win="background: white;border-radius: 5px;box-shadow: 0 1px 2px #666;left: 33%;padding: 2%;position: fixed;top: 33%;width: 33%;z-index: 1001;";$("head").append('&lt;style id="mw_style"&gt;#mw_mask{'+mw_mask+"}#mw_win{"+mw_win+"}&lt;/style&gt;");$("body").append('&lt;div id="mw_mask"&gt;&lt;/div&gt;&lt;div id="mw_win"&gt;'+a+"&lt;/div&gt;");$("#mw_mask,.mw_close").unbind("click").bind("click",function(){mw_close()})}</pre>
<p>Here&#8217;s the code without minifying:</p>
<pre>/* Simplest Modal Window by Xavi Esteve (Creative Commons BY-SA) http://xaviesteve.com/?p=3054 */
$(function() {
  /* Modal window script in 9 lines */
  function mw_load(html) {
    mw_mask = 'background:#333;height:100%;opacity:0.33;position:fixed;top:0;left:0;width:100%;z-index:1000;';
    mw_win = 'background: white;border-radius: 5px;box-shadow: 0 1px 2px #666;left: 33%;padding: 2%;position: fixed;top: 33%;width: 33%;z-index: 1001;';
    $('head').append('&lt;style id="mw_style"&gt;#mw_mask{'+mw_mask+'}#mw_win{'+mw_win+'}&lt;/style&gt;');
    $('body').append('&lt;div id="mw_mask"&gt;&lt;/div&gt;&lt;div id="mw_win"&gt;'+html+'&lt;/div&gt;');
    $('#mw_mask,.mw_close').unbind('click').bind('click', function() {mw_close();});
  }
  function mw_close() {$('#mw_win,#mw_mask,#mw_style').remove();}</pre>
<p>You can load a new Modal Window through JavaScript by using this function:</p>
<pre>mw_load('&lt;p&gt;Lorem ipsum dolor sit &lt;a href="#" class="mw_close"&gt;amet&lt;/a&gt;.&lt;/p&gt;');</pre>
<p>If you have any issues please let me know, note that the plugin&#8217;s purpose is to simply work, fancy features and extras should be coded apart specifically for your projects. The plugin requires jQuery and it hasn&#8217;t been tested in IE6.</p>
]]></content:encoded>
			<wfw:commentRss>http://xaviesteve.com/3054/simplest-modal-window-less-than-1kb-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixed Website Fold height (minimal Javascript Plugin, jQuery)</title>
		<link>http://xaviesteve.com/2088/fixed-website-fold-height-minimal-javascript-plugin-jquery/</link>
		<comments>http://xaviesteve.com/2088/fixed-website-fold-height-minimal-javascript-plugin-jquery/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 06:22:10 +0000</pubDate>
		<dc:creator>Xavi</dc:creator>
				<category><![CDATA[Front-End]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[fold]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://xaviesteve.com/?p=2088</guid>
		<description><![CDATA[FoldResizer is a Javascript plugin that will keep anything you want above the fold without worrying about your visitors screen size. The plugin is very easy to install and it works by resizing a &#60;div&#62; and allowing you to decide how much space left you need. Here are some examples: These are screenshots of the [...]]]></description>
			<content:encoded><![CDATA[<p>FoldResizer is a Javascript plugin that will keep anything you want above the fold without worrying about your visitors screen size. The plugin is very easy to install and it works by resizing a &lt;div&gt; and allowing you to decide how much space left you need. Here are some examples:</p>
<p><img class="alignright size-full wp-image-2089" src="http://xaviesteve.com/wp-content/uploads/2011/04/resizablefolds.jpg" alt="" width="600" height="171" /></p>
<p>These are screenshots of the same website loaded in different screen sizes, as you can see, the fold is always in the same place, displaying the same content for each user and having full control on what you want to show to them. Always keeping what matters <strong>above the fold</strong>.</p>
<h2>Features</h2>
<ul>
<li><strong>Cross-browser</strong> &#8211; Works in Firefox, Chrome, Safari, IE (including IE6)</li>
<li><strong>No plugins required</strong> &#8211; Just 100% pure Javascript</li>
<li><strong>Instant loading</strong> &#8211; No need to wait for the page to be loaded completely</li>
<li><strong>Small size</strong> &#8211; Less than 0.2KB</li>
</ul>
<h2>Instructions</h2>
<p>Include the script file and call the function by passing the parameters:</p>
<p><strong>foldresizer(&#8216;containerID&#8217;, freeheight, minheight);</strong></p>
<ol>
<li>ID of the container to be resized</li>
<li>How many pixels height to leave free</li>
<li>Minimum size of the resized container (default 0)</li>
</ol>
<h3>Example:</h3>
<pre>&lt;script type="text/javascript"&gt;
  resizefold('head', 200, 300);
&lt;/script&gt;</pre>
<p>Note that <strong>this plugin is purely JavaScript</strong> and does not rely on jQuery or any other plugins so you can call it in your &lt;head&gt; while the page is loading and <em>users will not notice any page redrawing</em> or resizing.</p>
<h2>Demo</h2>
<p>See the plugin working live at the <a title="SFP Group" href="http://www.sfpgroup.com">SFP Group website</a>. Resize your browser window and reload the page to see how it always keeps the fold fixed.</p>
<h2>Download</h2>
<p>Download the script by right clicking on the links and clicking &#8216;Save as&#8217;:</p>
<ul>
<li><strong><a title="jQuery Fold Resizer Minified" href="http://xaviesteve.com/files/foldresizer.min.js">FoldResizer Minified version</a> (0.95KB) (recommended)</strong></li>
<li><a title="jQuery Fold Resizer Editable code" href="http://xaviesteve.com/files/foldresizer.js">FoldResizer Maxified version</a> (1.5KB)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://xaviesteve.com/2088/fixed-website-fold-height-minimal-javascript-plugin-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clickable DIVs &#8211; jQuery plugin</title>
		<link>http://xaviesteve.com/1708/clickable-divs-jquery-plugin/</link>
		<comments>http://xaviesteve.com/1708/clickable-divs-jquery-plugin/#comments</comments>
		<pubDate>Tue, 10 May 2011 07:26:23 +0000</pubDate>
		<dc:creator>Xavi</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Front-End]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[UX]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[ie6]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://xaviesteve.com/?p=1708</guid>
		<description><![CDATA[Clickable DIVs jQuery plugin will convert anything (&#60;div&#62;, &#60;li&#62;, &#60;p&#62;, etc.) into a clickable object. It is a great way to enhance the usability of your website by increasing the clickable space to your users while avoiding extra HTML markup or a somehow tedious re-coding of your website. Works with JavaScript disabled too (graceful degradation) [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-2155" src="http://xaviesteve.com/wp-content/uploads/2011/05/untitled-3.jpg" alt="" width="600" height="200" /><br />
<strong>Clickable DIVs jQuery plugin</strong> will convert anything (&lt;div&gt;, &lt;li&gt;, &lt;p&gt;, etc.) into a clickable object. It is a great way to enhance the <strong>usability</strong> of your website by increasing the <strong>clickable space</strong> to your users while avoiding extra HTML markup or a somehow tedious re-coding of your website.</p>
<ul>
<li>Works with <strong>JavaScript</strong> disabled too (graceful degradation)</li>
<li>Works in all <strong>modern, mobile and old browsers</strong>: Firefox, Chrome, Opera, Safari, Internet Explorer (including <strong>IE6</strong>), touch screens (<strong>iPhone</strong>, iPad, Android), etc.</li>
<li>Detects if link needs to be opened in a <strong>new page or the same</strong> one</li>
<li><strong>1 line of code</strong>, easy to install (paste the code and add <code>click</code> class to the <code>div</code>)</li>
<li>Works with <code>div</code>, <code>li</code>, <code>p</code>, <code>h1</code> or any other HTML element</li>
</ul>
<p>This small plugin is being used in this website already. This is how my front page looks like:</p>
<p><img class="size-full wp-image-2139 alignleft" title="clickabledivs-withouttheplugin" src="http://xaviesteve.com/wp-content/uploads/2011/05/clickabledivs-withouttheplugin.jpg" alt="" width="600" height="361" /></p>
<p>The red area symbolizes the clickable area where the link would work for users visiting the website. The black square shows you the existing markup of each listed item.</p>
<p>By adding a simple <code>click</code> class to the list element, it inherits the click bind of the <code>&lt;a&gt;</code> in it and everything becomes a link. The plugin will also detect if the link is set to <strong>open in a new window</strong>. Look at the clickable area after enabling the plugin:</p>
<p><img class="alignright size-full wp-image-2140" title="clickabledivs-withtheplugin" src="http://xaviesteve.com/wp-content/uploads/2011/05/clickabledivs-withtheplugin.jpg" alt="" width="600" height="361" /></p>
<p>You can see a <strong>working demo</strong> at the bottom of this page.</p>
<h3>Usage</h3>
<p>You could add this plugin as an external file but since it is so tiny, the best way of doing it is to embed it into an existing <code>.js</code> file.<br />
1. Add this code inside the DOM ready function of your <strong>jQuery</strong>:</p>
<pre>/* Clickable divs - Copyright Xavi Esteve 2011 Dual licensed under the MIT or GPL Version 2 licenses. http://xaviesteve.com/clickable-divs-jquery-plugin */
$('.click').css("cursor","pointer").click(function(){var link=$(this).find('a:first');var linkhref=link.attr('href');if(link.attr('target')){var newWindow=window.open(linkhref,link.attr('target'));newWindow.focus()}else{window.location=linkhref}return false});</pre>
<p>2. Add the CSS class <code style="font-weight: 900;">link</code> to any element you want to inherit the click (a <code>&lt;li&gt;</code> for example).</p>
<h4>Source code</h4>
<p>Feel free to modify and improve the script by going to <a href="http://jsfiddle.net/xaviesteve/AEEeG/" rel="nofollow">ClickableDivs at jsFiddle</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://xaviesteve.com/1708/clickable-divs-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery-JavaScript: Basic Cheat Sheet for Beginners</title>
		<link>http://xaviesteve.com/383/jquery-javascript-basic-cheat-sheet-for-beginners/</link>
		<comments>http://xaviesteve.com/383/jquery-javascript-basic-cheat-sheet-for-beginners/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 08:51:34 +0000</pubDate>
		<dc:creator>Xavi</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Front-End]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[XHTML]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[cheatsheet]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://xaviesteve.com/?p=383</guid>
		<description><![CDATA[Content Check if element exists if ($('#infobox').length) { /*exists, do things here*/ } Change attribute $('#name').attr('title', 'Hello!'); Change content of a container $('#main').html('&#60;p&#62;Lorem ipsum&#60;/p&#62;'); Change page title document.title = 'Hello!'; Add content before/after $('table&#62;tbody&#62;tr:last').after('&#60;tr&#62;&#60;td&#62;This is now the last row.&#60;/td&#62;&#60;/tr&#62;'); Style Add or remove classes $('#main').addClass('red'); $('#main').removeClass('blue'); Check if element has class if ($('#main').hasClass('green')) { } [...]]]></description>
			<content:encoded><![CDATA[<h2><img class="alignright size-full wp-image-1994" src="http://xaviesteve.com/wp-content/uploads/2009/12/13.jpg" alt="" width="600" height="280" /></h2>
<h2>Content</h2>
<p><strong>Check if element exists</strong></p>
<pre>if ($('#infobox').length) { /*exists, do things here*/ }</pre>
<p><strong>Change attribute</strong></p>
<pre><code>$('#name').attr('title', 'Hello!');</code></pre>
<p><strong>Change content of a container</strong></p>
<pre><code>$('#main').html('&lt;p&gt;Lorem ipsum&lt;/p&gt;');</code></pre>
<p><strong>Change page title</strong></p>
<pre><code>document.title = 'Hello!';</code></pre>
<p><strong>Add content before/after</strong></p>
<pre><code>$('table&gt;tbody&gt;tr:last').after('&lt;tr&gt;&lt;td&gt;This is now the last row.&lt;/td&gt;&lt;/tr&gt;');
</code></pre>
<h2>Style</h2>
<p><strong>Add or remove classes</strong></p>
<pre><code>$('#main').addClass('red');
$('#main').removeClass('blue');</code></pre>
<p><strong>Check if element has class</strong></p>
<pre>if ($('#main').hasClass('green')) { }</pre>
<h2>Ajax</h2>
<p><strong>GET with parameters</strong></p>
<pre><code>$.get("page.php", { action: "edit", id: 38 },
  function(data){
   $('#infobox').html(data);
});</code></pre>
<h2>Loops</h2>
<p><strong>Do something with every matching element</strong></p>
<pre>$('table&gt;tbody&gt;tr').each(
  function (intIndex) {
    /* Modify all table rows */
});</pre>
<h2>Maths</h2>
<p><strong>Check if even or odd</strong></p>
<pre>if ($id % 2) { /*even*/ }else{ /*odd*/ }</pre>
<p><strong>Force string to integer</strong></p>
<pre>parseInt(var);</pre>
<p><strong>Filter words, numbers or symbols</strong></p>
<pre>$(this).attr('id').replace(/[^0-9]+/, '');</pre>
<p><strong>Timer</strong></p>
<pre>setInterval(timer1s, 1000);
function timer1s() { /*run this every second*/ }</pre>
]]></content:encoded>
			<wfw:commentRss>http://xaviesteve.com/383/jquery-javascript-basic-cheat-sheet-for-beginners/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Greasemonkey User Scripts Quick guide</title>
		<link>http://xaviesteve.com/219/greasemonkey-user-scripts-quick-guide/</link>
		<comments>http://xaviesteve.com/219/greasemonkey-user-scripts-quick-guide/#comments</comments>
		<pubDate>Sun, 26 Oct 2008 01:12:44 +0000</pubDate>
		<dc:creator>Xavi</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[greasemonkey]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[manual]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://xaviesteve.com/?p=219</guid>
		<description><![CDATA[This is just a quick guide for coding user scripts for Greasemonkey. You should know that a knowledge in JavaScript makes everything much easier. Then you just need to learn Greasemonkey functions to start coding. As a cheat sheet and reference, let&#8217;s see a little bit of basic JavaScript first: JavaScript Reference Creating Arrays var [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-2007" src="http://xaviesteve.com/wp-content/uploads/2008/10/monkey.jpg" alt="" width="600" height="280" /></p>
<p>This is just a quick guide for coding user scripts for Greasemonkey. You should know that a knowledge in JavaScript makes everything much easier. Then you just need to learn Greasemonkey functions to start coding.</p>
<p>As a cheat sheet and reference, let&#8217;s see a little bit of basic JavaScript first:</p>
<h3>JavaScript Reference</h3>
<h4>Creating Arrays</h4>
<pre>var people = new Array(
new Array("John","green","Doctor"),
new Array("Mike","red","Engineer")
);</pre>
<p><code>people[1][0]</code> will return <code>Mike</code>.</p>
<h4>Sending line breaks</h4>
<p>Use <code>\n</code>. It&#8217;s like <code>&lt;br /&gt;</code> in HTML.</p>
<h4>Search for a string</h4>
<p>Searches for a string (you can also use Regular Expressions). If it is found it will return 0 or more (which is the starting character number in the string). If it is not found: -1.</p>
<pre>q = "this is foo time";
q.search("foo");</pre>
<p>Will return <code>8</code>.</p>
<p>To search without matching case, use RegEx and add the &#8220;i&#8221; flag:</p>
<pre>q.search(/foo/i);</pre>
<h4>Substracting specific text from a string</h4>
<pre>q = "this is foo time";
q.substr(8,3);</pre>
<p>Will return <code>foo</code>.</p>
<h4>Quick notes</h4>
<p><strong>Get the URL:</strong> document.URL;<strong><br />
Redirect:</strong> document.location = &#8220;http://www.example.org&#8221;;</p>
<h3>GreaseMonkey Reference</h3>
<p>Let&#8217;s get into business now! :D</p>
<p>Coding is just like JavaScript. we can get information from the page as if the script code was inside it.</p>
<p>If we type var msg = document.getElementById(&#8216;message&#8217;).innerHTML; we will get the content from the element with message ID into our variable. We can work with it, store it or whatever.</p>
<h4>Permanent user script variables</h4>
<p>A nice feature is that you can store special GreaseMonkey variables. Will be stored permanently until the script is uninstalled.</p>
<p>To create or change <code>bank</code> variable with a value of <code>1000</code> we do:</p>
<pre>GM_setValue("bank", 1000);</pre>
<p>To get bank variable:</p>
<pre>GM_getValue("bank", -1);</pre>
<p>In case <code>bank</code> is not defined, <code>-1</code> will be returned.</p>
<h4>Inserting HTML code</h4>
<p>Once we finish our script thingies, we would like to insert the result into the page. First of all, we create an array with all the code:</p>
<pre>var menuCode = new Array();</pre>
<p>And fill it:</p>
<pre>menuCode.push('&lt;h4&gt;My menu&lt;/h4&gt;');
menuCode.push('&lt;li&gt;Home&lt;/li&gt;');
menuCode.push('&lt;li&gt;Forum&lt;/li&gt;');</pre>
<p>Once we get the desired code (note that you are filling a normal array, so that you can add as much JavaScript code as you want), we need to start using Greasemonkey&#8217;s functions:</p>
<pre>var menu = document.createElement('div');
menu.id = 'cheatsheet';
menu.innerHTML = menuCode.join('\n');</pre>
<p>Above, we have created a Div element named &#8220;menu&#8221;, supplied an ID and grabbed the menuCode array and joined it. Now that we have the menu Div created, it would be nice to empty the old array now.</p>
<pre>menuCode.length = 0;</pre>
<p>At last, we just need to insert the menu Div inside the page:</p>
<pre>document.body.insertBefore(menu, document.body.lastChild);</pre>
<p>Using <code>insertBefore</code> function, we grab the Div and insert it at the bottom.</p>
<h4>Inserting Script and CSS Style tags</h4>
<p>Similar to HTML but changing the way of insertion. Both CSS style and JavaScript code go inside the &lt;style&gt; or &lt;script&gt; tags inside the &lt;head&gt; so it&#8217;s the same way.</p>
<p>We create a temporary array and insert everything there:</p>
<pre>var scriptCode = new Array();
scriptCode.push('function spam() {');
scriptCode.push(' alert("This is my message box!");');
scriptCode.push('}');</pre>
<p>We create the Greasemonkey element, insert the code in it and empty the old variable:</p>
<pre>var script = document.createElement('script');
script.innerHTML = scriptCode.join('\n');
scriptCode.length = 0;</pre>
<p>Here&#8217;s the tricky part, we will use <code>try</code> to insert it inside the &lt;head&gt;:</p>
<pre>try { document.getElementsByTagName('head')[0].appendChild(script); }
catch(e) {}</pre>
<p><code>e</code> will store any error ocurred while inserting.</p>
<p>Styles are introduced the same way. Just add CSS code inside the array.</p>
<h3>Useful links</h3>
<ul>
<li><a href="http://userscripts.org/users/69777" target="_top">LuckyShot Userscripts.org profile (have a look at the favorited ones)<br />
</a></li>
<li><a href="http://wiki.greasespot.net/Tutorials" target="_top">Greasemonkey Tutorials and Resources</a></li>
<li><a href="http://www.oreillynet.com/lpt/a/6257" target="_top">Nice intermediate guide to avoid common pitfalls</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://xaviesteve.com/219/greasemonkey-user-scripts-quick-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

