« Posts under javascript

Compact JS code using PHP

This is a simple function to minimize JS code, it’s very simple and probably fail with more complex cases. But hey, might be useful for someone for something! We would suggest using JSMin::minify() instead.

jQuery.com down or super slow

Anyone knows a downloadable documentation for jQuery. Their site is so slow that makes referencing impossible. This is an recurring issue that has happened for quite a few months. Is the traffic too much for them to handle?

Check username availability via AJAX with jQuery



Server-side check.php

jQuery eq() typical mistake

With all the open/close quote, you might write this code:

var i = 123;

Since the selector string is within the quotes, the correct statement should be:


Ad Server with AJAX

Normally, JavaScript document.write is used by ad servers to write the ad code directly into the page (AdSense, AdSpeed, OpenX, etc.) To reload ads automatically without a page reload, we use AJAX with JSONP (JSON with padding). Partial content reloading uses DOM to update the content. jQuery 1.2.x has this built-in, or you can write your own library.

jQuery, Google AJAX APIs

We recently implemented jQuery into our ad server, majorly for future feature expansions and hopefully convert some of the existing JS enhancements using jQuery for a simpler codebase. Google AJAX API is nice but does not support SSL, which is quite inconvenient.

Nested JavaScript inclusions and IE

Consider the following scenario: file.html contains inline JS code to load remote.js and call a function in remote.js

And in remote.js

You would expect the output in this order and Firefox actually honors this order correctly:

  1. Before loading remote.js
  2. After loading remote.js
  3. In remote function

IE on the other hand will give you an error (“Object Expected”) and will call the function prematurely. Meaning that it executes before loading remote.js. This is an unexpected behavior. Fortunately, you can add the attribute defer=”defer”. This will specifically prevent IE from executing and actually produce proper result. Hmm, it’s IE! It has its own style!

JavaScript – setTimeout vs. setInterval

If you need to delay execution (like sleep function), use setTimeout. If you need to repeatedly do something (like refresh stats), use setInterval.

  • window.setTimeout(“show();”,2000); // wait 2 seconds then run show()
  • window.setInterval(“show();”,3000); // run show() every 3 seconds

Javascript regular expression in Opera

The following Javascript code works in Firefox & Internet Explorer but not in Opera. Different browsers interpret Javascript code differently.

$pTxt = $pTxt.replace(/(.|\n)+<\/tag>/g,'');

This code works in all three.
$pTxt = $pTxt.replace(/[\s\S]+<\/tag>/g,'');