Category: 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. /** compact JS code */ function compactJS($pContent) { $pContent = str_replace(“\r\n”,”\n”,$pContent); $pContent = preg_replace(“/^\/\/.*/”,”,$pContent); // begining w/ // $pContent = preg_replace(“/[^:]\/\/.*/”,”,$pContent);…

  • 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 Checking… JavaScript $(“#loading”).hide(); var orig=$(“#loading”).html(); $(“#UserNameInputID”).blur(function() { $(“#loading”).html(orig); $(“#loading”).show(); loadTextPage(“loading”,”check.php?username=”+$(“#UserNameInputID”).val()); }); /** 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…

  • 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 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…

  • 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)+/g,”); This code works in all three. $pTxt = $pTxt.replace(/[\s\S]+/g,”);