6 fast jQuery Tips: More basic Snippets

Posted 2009-01-07 in JavaScript by Johann.

Still hungry for jQuery tips? After the first and second session, here are more snippets.

Force a Page Reload

This will forcefully reload the current page. No jQuery needed here.

location.reload(true);

If your content might be cached by proxy servers and your URL has no query string, try this:

location.replace(
 location.href.replace(/\?.*$/, '') + '?' + Math.random());

Many ad networks use this trick to ensure that ad tags are never cached.

Try it: Reload this page.

Reload an Image

There is no reload or replace method for images. The src property can be set in the same way as the location.href property though.

with($('#image')) {
 src = src.replace(/\?.*$/, '') + '?' + Math.random();
}

Roßstein, Buchstein und Leonhardstein

Try it: Reload this awesome photo of my awesome tour to the Schildenstein. In Firebug, you will see the new image being loaded.

Replace a <div> with jQuery

The easiest way, pointed out by Paul, is to use the replaceWith method.

$('#thatdiv').replaceWith('<div>fnuh</div>');

A more confusing way is to insert the new element after the target element’s previous element and then remove the target element. The with statement makes it shorter.

with ($('#thatdiv')) {
 with (prev()) {
  after('<p>Your new content goes here</p>');
 }
 remove();
}

Try it: Replace this element with something completely different.

Verify if an Element is empty

if ($('#keks').html()) {
 // Do something to remedy the situation
}

Test it: Is this element empty?

Append or Add HTML to an Element

One of the nicest things about jQuery that most methods are named what you’d expect them to be named.

$('#lal').append("<b>lol</b>");

Test it: Append your eBay username and password to this.

When to load jQuery on the page?

I assume most people include the jquery.js in the <head> portion of a page – at least I do. But unless you write CSS overrides on the page from JavaScript (document.write('<style>/<script>…')), putting it at the bottom improves performance.

I hope you have enjoyed these snippets. If you did, get updates via my news feed.

3 comments

Java Wildcard String Matching

Posted 2008-12-15 in Java by Johann.

This entry contains code examples of Java pattern matching with wildcards.

Note that wildcard matching is not the same as the .* regular expression which matches any number of characters – a wildcard matches only one character. Wildcards are usually encoded as ., but the actual value may vary across libraries.

StringSearch

StringSearch 1.2 comes with two wildcard pattern matching algorithms, BNDMWildcards and ShiftOrWildcards. Generally, BNDMWildcards will be faster, which is why I removed ShiftOrWildcards in version 2.

public void testStringSearch() {
    BNDMWildcards bndm = new BNDMWildcards();
    Object compiled = bndm.processString("bla.blorb");
    // "bla?blorb" for StringSearch 1.2
    assertEquals(3,
        bndm.searchString("la bla0blorb null", "bla.blorb", compiled));
}

java.util.regex.Pattern

The java.util.regex.Pattern API isn’t very compact, but of course it does offer more than just wildcards.

public void testJavaUtilRegex() {
    Pattern searchPattern = Pattern.compile("bla.blorb");
    Matcher m = searchPattern.matcher("la bla0blorb null");
    assertTrue(m.find());
    assertEquals(3, m.start());
}

Jakarta Oro

ORO offers many PatternMatcher implementations. In this example, I am using the Perl5Matcher class.

public void testJakartaORO() throws MalformedPatternException {
    Pattern p = new Perl5Compiler().compile("bla.blorb");
    Perl5Matcher matcher = new Perl5Matcher();
    assertTrue(matcher.contains("la bla0blorb null", p));
    MatchResult result = matcher.getMatch();
    assertEquals(3, result.beginOffset(0));
}

Case Insensitive Search

All of the APIs presented here support case-insensitive string matching. The case insensitive option is simply compiled into the pattern in the compile phase.

StringSearch

This example requires StringSearch version 2 or greater.

    BNDMWildcardsCI bndm = new BNDMWildcardsCI();
    Object compiled = bndm.processString("bla.blorb");
…

java.util.regex.Pattern

    Pattern searchPattern = Pattern.compile("bla.blorb",
        Pattern.CASE_INSENSITIVE);
…

Jakarta ORO

        Pattern p = new Perl5Compiler().compile("bla.blorb",
                Perl5Compiler.CASE_INSENSITIVE_MASK);
…

Roland RSS-10 Software and Manuals

Posted 2008-12-11 in Effects by Johann.

Roland RSS-10 and more

At the top of this picture is a Roland RSS-10 Sound Space Processor. The RSS-10 is one of the most unique signal processors ever – it simulates three-dimensional audio. Sounds can be placed above, behind, away from and even below the listener at various distances.

Using a conventional two-channel system, sound can be placed or moved anywhere in the three-dimensional field. Computer controlled sound localization is accomplished using specialized software for either Macintosh® or Windows®.

  • Sounds can be moved above, away, towards or around the listener
  • Conventional two-channel playback
  • Simulates flanging and Doppler effect
  • Windows or Mac control software
  • Up to 16 RSS-10s can be linked for 32 channels of 3-D playback
  • Discrete stereo In/Out

When it came out at the beginning of the 90’s, these algorithms had to be run on DSPs.

Using a conventional two-speaker stereo reproduction system, a full three-dimensional sound image is created. The RSS-10 is used during the stereo mixdown process, with no encoders or decoders needed for final stereo reproduction.

The RSS system comprises a binaural processor which is the heart of the RSS system, a transaural processor and a distance processor that reproduces the distance from the sound source, and a reverb processor that simulates the sound reflection from any direction and creates realistic and precise reverberations.

A Stationary mode locates the sound image at a stationary position, while a Flying mode moves the sound image in real time. Both modes can be easily controlled on a personal computer (Mac or Windows) using software packages that come with the system.

An extremely interesting concept – that never really caught on.

If you would like to read more about the RSS-10, I once wrote a review for Harmony Central. There's also an FTP site that Roland set up.

Over the years, I was asked several times for the software or the manuals. Instead of emailing the files over and over, I decided to put them here on my blog.

Note that the Mac software disk was not extracted by me. My disk was broken and I could never read it. I have no idea if it works and what the disk contains.

Alternatives

If you cannot find an RSS-10 – some of it has made it into various Roland and BOSS products. But these algorithms only support a fraction of the possibilities of the RSS-10. Here are some alternatives that you might be able to get:

Prosoniq Ambisone

I think that Ambisone is just as interesting to expand the stereo space as the RSS-10. It’s easier to use, but maybe not quite there in terms of quality over speakers.

QSound

I have the original DirectX QSound plugin. QSound is very effective at making the sound stage wider, but it cannot place sounds anywhere around the listener.

CORBA Packet Sniffing/Class Patching Hack

Posted 2008-12-08 in Java by Johann.

This is a nasty, ugly hack for situations where you are forced at gunpoint to deal with the abomination that is CORBA. It lets you see incoming CORBA data – without using an actual packet sniffer/network protocol analyzer.

What we will be doing is:

  1. Take the source from one JDK class
  2. modify it and
  3. place it before the actual JDK class with the -Xbootclasspath/p: switch.

In essence, this lets you patch or edit any JDK or application class, which is good for a variety of scenarios, including closed-source software.

The Class

The class to patch is com.sun.corba.se.impl.encoding.EncapsInputStream. This class is responsible for receiving the GIOP-encoded CORBA data.

The original constructor code:

    public EncapsInputStream(org.omg.CORBA.ORB orb, byte[] buf, 
                 int size, boolean littleEndian,
                 GIOPVersion version) {
        super(orb, ByteBuffer.wrap(buf), size, littleEndian,
          version, Message.CDR_ENC_VERSION,
          BufferManagerFactory.newBufferManagerRead(
                      BufferManagerFactory.GROW,
                      Message.CDR_ENC_VERSION,
                      (ORB)orb));

    wrapper = ORBUtilSystemException.get( (ORB)orb, 
        CORBALogDomains.RPC_ENCODING ) ;

        performORBVersionSpecificInit();
    }

The Modification

Modifying this class involves creating the ByteBuffer in a static method and printing the data contained in buf there. Something like this:

    private static ByteBuffer wrap(byte[] buf) {
        System.out.println(new HexDumpEncoder().encode(buf));
        return ByteBuffer.wrap(buf);
    }

    // corba/EncapsOutputStream
    // corba/ORBSingleton
    // iiop/ORB
    public EncapsInputStream(org.omg.CORBA.ORB orb, byte[] buf, 
                 int size, boolean littleEndian,
                 GIOPVersion version) {
        super(orb, wrap(buf), size, littleEndian,
          version, Message.CDR_ENC_VERSION,
          BufferManagerFactory.newBufferManagerRead(
                      BufferManagerFactory.GROW,
                      Message.CDR_ENC_VERSION,
                      (ORB)orb));

    wrapper = ORBUtilSystemException.get( (ORB)orb, 
        CORBALogDomains.RPC_ENCODING ) ;

        performORBVersionSpecificInit();
    }

The Launch

With the class patched, we need to prepend the directory or JAR it is located in to the bootstrap classpath.

>java -X
…
    -Xbootclasspath/p:<directories  ; by separated files jar zip and>
                      prepend in front of bootstrap class path

If your classes are compiled to the bin directory, -Xbootclasspath/p:bin is enough. You might need some libraries which can be appended to the bootstrap class path like so -Xbootclasspath/p:bin:lib/junit.jar:lib/wljmsclient.jar.

The Result

Each CORBA packet that your computer receives is neatly dumped to the console.

Pages

Page 1 · Page 2 · Page 3 · Page 4 · Page 5 · Page 6 · Page 7 · Next Page »

Subscribe

RSS 2.0, Atom or subscribe by Email.

Top Posts

  1. DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
  2. 6 fast jQuery Tips: More basic Snippets
  3. xslt.js version 3.2 released
  4. xslt.js version 3.0 released XML XSLT now with jQuery plugin
  5. Forum Scanners - prevent forum abuse
  6. Automate JavaScript compression with YUI Compressor and /packer/

Navigation