Archive for category jQuery
ASP.net update panel’s and jQuery
Posted by shawson in .net, Javascript, jQuery on July 13th, 2010
I had a problem today with buttons in an update panel and jQuery- there are some buttons which get hidden or displayed depending on the user’s OS – mac or pc users basically get shown different instructions- However because these buttons are added by a partial post back of the update panel, the jQuery document.ready had already fired before these buttons existed.
So I experimented adding the code to the form.submit event but in the end found the solution on google; I simply added this;
<script type='text/javascript'>
function pageLoad(sender, args)
{
// your code here..
showHideMacLinks();
}
</script>
This handles the pageLoad event raised by my update panel which is fired on the initial load and on the partial post back. You can filter this to only fire on partial post back by adding “if(args.get_isPartialLoad())”
Found this over on the .net Funda blog
HTML5 Audio and Video (Media) Tags – How do you know when the data’s loaded? And how to play multiple instances of the same sample at the same time
Posted by shawson in HTML5, Javascript, jQuery on June 10th, 2010
Quick post with a snippet of code I’ve used for the 8 week game competition I’m in- I’m going to be using html5 audio element for all my game’s sounds and needed a way to make sure the samples were totally loaded so the resource manager knows when to hand control to the game- So after flicking through the html5 spec came up with this code (utilising jQuery to bind my event handler)
Note- This applies to any media object- so the video tag as well, but I’ve only tried this with audio.
// http://www.whatwg.org/specs/web-apps/current-work/#mediaevents
// http://api.jquery.com/bind/
var ao = new Audio();
$(ao).bind('canplaythrough', function() { // totally loaded
alert('ready to play');
});
ao.src = 'sounds/my_sound.wav';
ao.load();
The above example will wait until the media element has enough data to comfortably play through without stopping to buffer. if you wanted you could also bind to a whole host of other events in exactly the same way, which you can read about here. You can check the status at any time to see how loaded the audio/video file is like this;
ao.readyState;
This will return you back a number between 0 (HAVE_NOTHING) to 4 (HAVE_ENOUGH_DATA) – full info can be found here.
Because I’m building a game I have slightly different requirements to the average web project- The way I use this is have a central “ResourceLoader” which I load up with all the url’s to the asset’s I’ll be using, this then reports when all the assets are totally loaded and hands control to the game. So there is a single instance of every image and audio element I’m using- I then hand each loaded sound to the sound manager to play. This raises a problem when i want to play the same sample more than once, overlapping because you are essentially just passing around reference to the same audio object- so i execute the play method, then execute it again and nothing happens because it is already playing! The answer is to perform a deep copy of the audio element before playing- because this is a DOM object you can do this easily like so;
// https://developer.mozilla.org/en/CloneNode var instance1 = ao.cloneNode(true); var instance2 = ao.cloneNode(true); instance1.play(); instance2.play();
This way you can hit the play() method of instance1, and the instance2 and they will both play at the same time, totally independant of each other, even though they both originate from the pre-loaded instance ao.
Anyway- you can see live demos of this here;
Notice the need for 2 demos because chrome cant read wav and mozilla wont do MP3! Ogg vorbis is a common format for the two, but i couldn’t quickly find any example files to use! Read here for browser file format support.
These demo’s were made for us in the 8weekgame site – bare in mind the sound manager class I literally started about an hour ago so it’s got hardly any functionality but it does play sounds so take a look at the source code and hopefully it will all make sense!
New site launch- natashahampshire.co.uk
Juts launched a new portfolio site for my girlfriend, www.natashahampshire.co.uk – This is just the first phase with a few more features and a bunch of extra contact to be added in the coming weeks, but we needed to get it up ASAP so she can apply for a design course in London!
Javascript & CSS Minification
Posted by shawson in CSS, Javascript, jQuery on April 28th, 2010
Is that a real word?
Well anyway, I’m just finishing off a fairly jQuery heavy site I’m building for my girlfriend as an online portfolio site and figured I’d minify the css and jQuery to reduce loading times (as all of my javascript includes combined come to around 100k)- found a really good java app writen by Yahoo(!) called the YUI Compressor – I combined all my javascript into one combined.js file then minified it nocking 30k off the file size. Did the same with css only making a saving of 2-3 k, but every little helps! Very easy to use, but it means you do have to install the java runtime
JQuery Accordion Control links not working
I recently swapped the .net Accordion control on my main home page from the Ajax Control toolkit for the jQuery Accordion control from jquery.com- this was mainly because i have a big moving flash background of the sky wooshing past, and found the MS accordion control a bit too heavy to expand out smoothly on slower machine, so thought i would try the jQuery version and was pleased to find it was a lot smoother.
However, to my horror, all my links stopped working!
$(function() {
$("#menu").accordion({
active: false,
alwaysOpen: false,
animated: "easeslide"
});
});
<ul id="menu" style="width: 250px;">
<li>
<a href="#">Welcome</a>
<div>This is the home page to what will eventually be a whole bunch of different web projects that i have online to fiddle about with.</div>
</li>
<li>
<a href="#">Work</a>
<div>
<ul>
<li>
<a title="My Web Development Blog" href="http://www.shawson.co.uk/codeblog/">My Code Blog</a>
</li>
</ul>
</div>
</li>
<li>...</li>
</ul>
This is because the jQuery accordion, using the default settings as i was, treats links as buttons for expanding the sections of the accodion, so voids their default action. I resolved this by updating the jQuery settings to only treat links with a specific class as the accordion headings, and leave other links alone- i created a new class called accordion-label to use for the accordion buttons and updated the jquery as follows;
$(function() {
$("#menu").accordion({
active: false,
alwaysOpen: false,
animated: "easeslide",
navigation: true,
header: "a.accordion-label"
});
});
I then added the accordion-label class to my “Welcome” link, “Work” link etc, and left the rest as-is.
JQueryUI
As most of my colleagues will know, I love JQuery and one of our contractors, Lee, today showed me the brilliant JQueryUI library which features a whole bunch of functionality similar to the horribly bloated .net control extenders. Well worth a look over at http://jqueryui.com/