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
HTML
JavaScript
/** load a text page into this div */
function loadTextPage($pDivID,$pURL) {
$.ajax({
url:$pURL,
dataType:’html’,
success:function (txt) {
$(‘#’+$pDivID).html(txt);
}
});
Server-side check.php
jQuery eq() typical mistake
With all the open/close quote, you might write this code:
var i = 123; $(":checkbox:eq(i)").removeAttr("disabled");
Since the selector string is within the quotes, the correct statement should be:
$(":checkbox:eq("+i+")").removeAttr("disabled");
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
alert('Before loading remote.js');
document.write('
alert('After loading remote.js');
document.write('
And in remote.js
function remoteFunction() {
alert(‘In remote function’);
}
You would expect the output in this order and Firefox actually honors this order correctly:
- Before loading remote.js
- After loading remote.js
- 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(/
This code works in all three.
$pTxt = $pTxt.replace(/