Category Archives: JavaScript

Javascript Code Samples

Using Google Analytics Or Other Javascript With Smarty Template Engine 0

On a website I was working on recently I added the Google Analytics tracking code to the footer of a Smarty template, like this: footer.tpl: 1234567891011121314151617<script type="text/javascript">   var _gaq = _gaq || [];   _gaq.push(['_setAccount', 'UA-XXXXX-XX']);   _gaq.push(['_trackPageview']);   (function() {     var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;     ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;     var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);   })(); </script> </body> </html> However, since the javascript used by Google Analytics includes { and } tags, also used by the Smarty template engine, it tries to interpret this code and depending on your settings will either fail silently or or with an error such as this: Smarty error: [in footer.tpl line 148]: syntax error: unrecognized tag ‘var’ The fix is simple. Enclose your Google Analytics code, or other javascript code, with {literal} and….

Secure Your Google Apps (Gmail, GDocs, GCal, etc) Comments Off

If you are a Google user – meaning Gmail, Google Docs, Google Calendar, Google Reader, etc – then you should know that by default, once you log in your sessions are typically not encrypted between your browser and the Google servers. For some more technical information on this, check out dmiessler’s post on the subject. He mentions using bookmarks to make force your browser to use Google’s secure connections – however I’ve noticed that occasionally depending on how you arrive to your Google services that you will switch to an unencrypted session without warning. For that reason, If you are using Firefox and greasemonkey, I highly recommend installing the “Google Secure Pro” userscript. It automatically switches you from using http:// to https:// to ensure your data is encrypted to Google’s servers. This will increase your security greatly from using the unencrypted connections, which is good if you transfer confidential data….

Closing a Firefox Window with Javascript 46

I recently had a problem where I was opening a browser window from an e-mail client. After taking a survey, I wanted a button to close this same window. In IE, it was fine to simply use window.close(); to close the window. However, Firefox, the evil genius that it is, will not let you do that because it believes you did not click to open this window in the first place. In order to trick it, you quick load a blank page then close it. The code: 12<a href="javascript:window.open(”,’_parent’,”);window.close();"> Close this window</a> So now, when you click the link, it closes the browser window. It works in both IE and Firefox 1.5 (and I believe others as well, but not personally tested). Finding that snippet of code saved me a lot of time! So I hope someone else finds it useful. Update, May 1 2008: This code only works on….

Greasemonkey + Duggmirror Script = Awesome Comments Off

Don’t you hate it when you try to visit a digg story and the site has been dugg? Use the greasemonkey firefox plugin and this duggmirror greasemoney script to add three links to each digg entry – the duggmirror archive, Coral NYU Mirror, and the Google Cache of the page, all with a nice graphical interface. It’s easy too: Install Firefox (you mean you haven’t already?) Install Greasemonkey plugin Restart Firefox Visit the duggmirror script‘s page and click “install this script”. A little box will pop up at the top of the browser screen, asking you for permission to install it. That’s it! When you visit digg – you will see the extra links next to the post title. It works in any OS that firefox support (I’ve personally tested on Windows XP, Vista, and Ubuntu).