Oliver Spryn
Oliver Spryn

Reputation: 17368

jQuery Plugin to Update URL Hash As Text Input Changes

Is there a jQuery plugin which will update the URL hash of a page as the user types text into a text input?

I know that this is rather simple, but I am looking for a "smart" one, such as Google Instant search or caniuse.com, which doesn't update the hash on every keyup() event, but rather when the user has stopped typing for a given period of time or the text input blurs.

I also know I could sit down and write this plugin myself, as I have written several before, but I was hoping someone could suggest one that has already been written so I don't have to reinvent the wheel.

Thank you for your time.

Upvotes: 1

Views: 708

Answers (2)

Jeff B
Jeff B

Reputation: 30099

Not that difficult. Untested code:

var hashTO;
$('#myTextField').on('keyup', function() {
    if(hashTO !== undefined) {
        clearTimeout(hashTO);
    }
    hashTO = setTimeout(updateHash, 1000);
});

function updateHash() {
    var hashVal = encodeURIComponent($('#myTextField').val());
    window.location.hash = hashVal;
}

Demo: http://jsfiddle.net/vent6/show/

Code: http://jsfiddle.net/vent6/

Upvotes: 2

Daniel Bang
Daniel Bang

Reputation: 725

Check the debounce method of Underscore library (of lodash, for that matter)

http://underscorejs.org/#debounce

Upvotes: 0

Related Questions