E-com DevBlog Spider-ball-vacuum

25May/090

Introducing jTrace v1.0

I've been working on a tool for javascript developers. It all started after searching the web for some form of tracing tool I could use when writing javascript so I could see why a certain function wasn't firing. I only found a handful of solutions (and none that I was very impressed by). So I finally ended up coding one myself.

So I give you, jTrace - javascript tracing for the peoples (say that last part like strong-bad to get the full effect).

I coded jTrace with jQuery 1.3.2 (as an include... not so much a plugin, although that's what I usually call it), and then I created a 'bloated' version that has jQuery included inside of it (for all you mooTools and prototypers out there... I got your back ;) ). For those who use the bloated version, yeah I coded the jQuery portion to use 'jQuery(...' instead of '$(...' for the selectors so it shouldn't break your exisiting code.

To get an in-depth approach on jTrace and how to use it, head over the 'usage' page on the jTrace site.

Have questions, comments, or props? Leave them here in the comments, thanks!

Print This Post Print This Post
28Apr/092

Pop-ups, the interweb, and you

The problem*: You have a page with a pop-up, and when you click a link on it, it needs to change the page that spawned it.

I searched everywhere and every result listing let me down. Site after site was bitter disappointment, I searched for 'pop-up close child window redirect parent', then 'pop up change parent', and 'pop up child link change parent window'. I did find some useful code like the 'onunload' event (we'll cover that another time ;) ). I finally stumbled onto the solution by mashing up a few different solutions that were posted for javascript events and the mysterious 'window' method built into the base js library*².

The solution (#1): This solution is for those who want to keep the pop-up open, or just want parent only manipulation (yeah, you can always manipulate the child (pop-up), but solution 2 is soooo much easier for that).

First, let's build a method to handle pop-ups.

?View Code JAVASCRIPT
function popWindow(url, myWidth, myHeight, myScrollbars, myTop, myLeft, myMenubar, myResize) {
	window.name = "main"; /* setting this is the key to letting you manipulate the parent window */
	window.open(url, "","width="+myWidth+",height="+myHeight+",scrollbars="+myScrollbars+",top="+myTop+",left="+myLeft+",menubar="+myMenubar+",resizable="+myResize+",bgcolor=#ffffff");
}

Note that we used "window.name" and set it to "main".  This allows me to reference the parent window from any child windows (pop-ups).  Now that we've done that, in our pop-up window we just need to make the link have a target of "main".  Like so:

<a href="http://fakedoman.com/newpage.html" target="main">Change the parent window!</a>

Now this comes together on your parent page, you click a link that uses the popWindow function that creates the pop-up that can now reference the parent by the name "main".

The solution (#2): This is the show, the heat, the expletive, the new hotness, and the fresh burnination. Ok, fine, it's old code that's been around forever that everyone on the internet forgot about (or at least didn't blog, write, post, etc).

In your pop up window, build a method like this in your script section (or included js file).

?View Code JAVASCRIPT
function openParent(webAddress){
	opener.location.href=webAddress;
	window.close();
};

Wow... 2 lines (4 if you count the open and closer of the function), simple, easy. Why has no one told anyone else about this*³ ? Now the only thing left is to call that function in an "onclick" event in your a-tag.

<a href="javascript:void(null);" onclick="javascript:openParent('http://fakedomain.com/newpage.html');"/>

Yes, I pointed the href to "javascript:void(null);", the onclick event will overwrite the original href call anyway, so this is how you build the link to keep the code valid to W3c standards.

And that, my friends, is how you code a pop-up window that will change the parent window with a link from the pop-up window.

 


 

* Yes I heard all of you grumblers in the back saying 'change the target in the a-tag to "_top" and that'll solve your problem. And you know what? yeah, in 1998 that would have worked. However these days our pop-ups are javascript based, and not so frames/iframe-y.

*² This statement could very well be false... in fact it's made up, I'm not sure if it's built in, I really can't back that up, yeah, total b.s. and yet the code works in IE6, IE7, IE8, Opera 9.63, Safari 3, Firefox 2, Firefox 3, and Chrome... so that kinda seems "built-in", or at least understood and interpreted correctly by most browsers.

*³ It could be that pop-ups in legitimate sites really aren't all that common, and generally the people who use them on their sites are spammers/malware/spyware vendors and should be dragged out in the street and beaten while having their selling slogans chanted and shouted at them (isn't that a pleasant picture you just painted in your head? your welcome).

Print This Post Print This Post
20Apr/090

Time for more jQuery goodness

No, it's not what you think.  I'm not posting some grand tutorial on how to re-animate the dead with jQuery... at least not yet.  What I am going to do is post a few things that I either stumbled into, or had to figure out for myself while using jQuery.

1st - Sometimes you may find yourself writing huge functions, or the like, in which you continually use the selector " $(this) ".  There's nothing wrong with that, I use it all the time.  However something you really should keep in mind is that every time you call that selector, you are throwing more drag on the cpu/browser/rendering of your site.  How do you alleviate this problem? simple, just set the $(this) selector into a variable.

?View Code JAVASCRIPT
/* how not to code */
$(document).ready(function(){
$(this).doSomething();
$(this).click(function(){
$(this).runMore();
});
$(this).yepEvenMore();
});
 
/* how you should code */
$(document).ready(function(){
var $this = $(this);
$this.doSomething();
$this.click(function(){
$this.runMore();
});
$this.yepEvenMore();
});

2nd - This really should be a no-brainer, but after spending over 15 minutes trying to get the selector to work (and browsing several jQuery sites, checking my cheat sheets, etc.) I finally found the answer to using a selector within a selector. I believe the answer came from the jQuery google-group. It was as simple as the following example.

?View Code JAVASCRIPT
$("#left label[for=" + $this.attr("id") + "]").addClass('active');

You can see that I had a div with an id of 'left' and I wanted a particular label inside that div to have a class of 'active' applied to it. This worked with a click event (not shown).

3rd - What happens when you have some images that when clicked you want to change the value of a form? Say for example that you have some logo's of credit cards that when clicked on you want to change a select box in your form to show the value of that credit card? The solution for this one I couldn't find anywhere on the interweb, so I spent plenty of time figuring this one out. It works, and I'm fairly happy with it. NOTE: you must be using jQuery 1.3+ to make this work, the changes made in this version of jQuery allow you to do this type of selector.

?View Code JAVASCRIPT
$("#payment-type-ful img").click(function(){
	var card = $(this).attr('alt');
	$("#policyId").val(card);
});

Yep, there's a div with an id of 'payment-type-ful' and I need any images under it, and when clicked on I set up a variable called 'card' that is set to the alternate text of the clicked-on image. I then select the input select and set the current value to 'card'. Simple? oh yeah, easy, absolutely.

That sums up a few quick little jQuery gems that I've been using recently. As per usual, if you have any questions, comments, or tips of your own, feel free to throw them in the comments, thanks!

Print This Post Print This Post
31Mar/091

LinkPost – March 2009

A new feature I've decided to put on the DevBlog is something I like to call the "LinkPost". This is mainly a collection of links that I either followed or used during the month. I've kind of done this before (massive posts with tons of links in them), but those posts weren't quite the same as this. Hopefully you'll find this useful, and of course if you found any interesting links during the month, feel free to throw them in the comments. Oh, and these are not sorted at all, just a random grab-bag of where I went, and what I looked at.

Note: this post is rather long, so I've kept the whole post off of the main page, click the 'read more' link below to see the entire post/list

Print This Post Print This Post
17Mar/091

Object Doesn’t Support this property or method

Just a quick post to spread more love about IE. Occasionally I run into an error on IE (who doesn't) that boggles my mind, like reading toaster directions when they throw in the verticle concept. Anyhow this particular one I've seen before but not sure I've ever known what causes it. I tried to click a submit button that when clicked is supposed to run some validation on the form. Works great in FF2 and 3 but not so much in IE, instead I get this error "Object Doesn't Support this property or method" then it attempts to give me a line and character number to look at but it's way off (as you may have guessed). So I googled this baby and the first post I read on it was the helpful-est. so I'll link to it for you pleasure but also give you the quick and dirty (too late). basically this happens if one of your js functions has the same name as one of your HTML ID's or it can also happen if a JS variable has the same name as a Form Element name which was what was happening in my case. Easy fix just change the name of one or the other and your all set.

Here is the post

Print This Post Print This Post
Tagged as: , , 1 Comment
23Feb/090

Quickpost: nyroModal – another jQuery plug-in to keep an eye on

Over the last week or three Internal Apps had a problem where they wanted to display a page, but have a window appear with new contract terms visible. They also needed a scroll bar, buttons to accept/decline, print, and close the pop-up. They also needed a little twist, if the user declined the new contract, or closed the pop-window, the page should send them back to the log in page.

I thought about the problems, and I was thinking recommending to them FancyBox (our old standby for modal windows), but I had just read on another site about nyroModal. I recommended that they try it out (since it's jQuery you have all those great callback functions - that would cover the page redirection etc. - and those oh-so-tasty visual effects that would provide them with exactly what they had envisioned), and within a day they had a perfect set up. So if you find that you need a modal box (for whatever reason), I suggest you give nyroModal a try. We ourselves won't be dumping FancyBox anytime soon, but it is nice to have another option.

Print This Post Print This Post
3Feb/092

Form Contorls – And some jQuery solutions

I was researching a little Twitter meets jQuery and I stumbled on this blog post. While it was helpful, and I may consider using the plugin, it was what the developer had 'tweeted' about that caught my eye and caused this post to exist.  You see he had posted two links to form controls.  The first link was ideas/new controls, and the second was the jQuery solution to those form controls.  Since our interal apps department made the switch finally started using jQuery -as it was pointed out how easy it makes things and how it can improve their existing code (smaller, better, faster)- last week from in addition to prototype to jQuery, I thought they would get a little added bonus out of this post (since they use a ton of form controls).

So first up, a link to a nice form controls gallery (it even posts which js libs can accomplish such feats)

Form Control(ery)s

And secondly a link to the jQuery solutions to each of the form control interfaces

jQuery Form Control Solutions

Between those two pages, you can dress up any page, form, or what-have-you to do almost anything you want.  And it will look like the fresh-burnination* doing it.

Another short and sweet post.

---------------------------------

*fresh-burnination is a tribute to strongbad, and the Men In Black, with a little CW (WB) mockery thrown in.  It is derived from the "new hotness", strongbad style, or the "fresh burnination".

Print This Post Print This Post
7Jan/090

IE6 and form submit fixery

Yesterday presented a problem I was unfamiliar with, and while ie6 continues to be the bain of all web developers existance, I finally was able to emerge triumphant, with the head of the demon tucked nicely under my arm... for now. The problem of course is trying to submit a form through javascript with ie6.

After poking around the large interweb I stumbled on another site that had run into the same problem. a slightly more indepth description of the problem is that you have a form, but rather than use a submit button, you have an image wrapped in an a-tag that you have assigned a click-event too that submits the form (or sends it to some js validation, and then submits the form). This is all fine-and-dandy with any 'modern' web browser, but for some reason that only m$ can tell us, when ie6 follows the a-tag, it gets lost without ever submitting the form, it doesn't even attempt to load the page.

Some people have reported that adding an empty anchor to your a-tag fixes the problem (you know, a simple >a href='#'<), but we like code that validates, so we usually insert ye olde 'javascript:void(null);' into our a-tags when we handle them with onclicks, or jQuerify them.

The solution that we ended up using was to use a setTimeout(); around the form submit in the js. For some reason, this allows ie6 to remember what it was supposed to do and get it done. I like to think of it as a virtual kick in the teeth to the browser. Anyway, here is some sample code to help you visualize the problem and solution.

Here is our form, (yes, it has a smattering of JSTL in it ;) , you can ignore that if it bothers you)

?View Code HTML4STRICT
<div class="review-form-input">
	<a href="javascript:document.reviewForm.submit();">
		<img src="<c:out value="${jspStoreImgDir}" />en_US/submit.gif" alt="Submit" />
	</a>
</div>

And here is our sample javascript code BEFORE being fixed (note the jQuery goodness, that I can't use enough of)

?View Code JAVASCRIPT
$(document).ready(function(){
	$(".submit a").click(function(){
		$("#reportForm").submit();
	});
});

Note that the above code works fine in Opera 9+, Safari 3+, Firefox 3+ (and most-likey but untested in Firefox 2+), and yes even the black-sheep of the browser family ie7 can render and run this code just fine.

Here is the fixed javascript...

?View Code JAVASCRIPT
$(document).ready(function(){
	$(".submit a").click(function(){
		setTimeout('$("#reportForm").submit();', 50);
	});
});

Yep, that's it, a very subtle change, just inserting the setTimeout() method fixes the ie6 stupidity.

Print This Post Print This Post
19Dec/080

DD_belatedPngFix – A new solution to an aging problem

This has already been reported on several tutorial/dev sites, but I thought I'd put in a word or two about it.  DD_belatedPngFix is a fantastic little piece of work created by Drew Diller.  Drew has taken a unique approach to fixing the many issues that plauge ie6 and png files.  Instead of the standard alphaimageloader he uses just a touch of VML to tweak the images into appearing correctly, this allows for png files to be used as backgrounds and eliminates the annoying problem of trying to click on an image/div/whatever that was in another div with a background set to a png.

I started to set NordicTrack up with this script, but found that it's not quite perfected with objects that have absolute positioning (if anything on the page is a png that is above where your positioning the absolute element, they will build themselves as blocks, then knock each other out of the way), or with png sprites (your roll-over image is zoomed in a little so it appears 'cut-off').  Drew is regularly updating his script (which is awesome) so hopefully he'll have these issues fixed soon. --UPDATE (03/19/2009)! So after doing a bit more research (and a few updates to the js from Drew) this may no longer be an issue. If you remove the "position: relative, z-index: 1" from your png elements (or containers) the dd_belated png fix will work like a champ! NordicTrack has been converted and has been running dd_belated for just over a month now (so go and firebug the code ;) and see how I made it work) with no crazy formatting errors!

So in short, if you need a png fix that is simple, easy, and you don't want to throw "position: relative; z-index: 1" on all your elements that have a png in them (or as a background).  This is definetly something you'll want to check out.

Here's that link again, in case you missed it -> http://www.dillerdesign.com/experiment/DD_belatedPNG/ .

Print This Post Print This Post
19Nov/082

Web Developer Training – jQuery

And now for our quarterly Web Developer Training (because monthly just wasn't happening).  This quarters training is on the infamous (and my personal favorite js framework) jQuery.  After recieving some requests for a training session on it, I finally got one put together and hope that it benefits everyone in some way.  Also this training is sampling my new training template (not that the old team awesome template wasn't good enough, it just... well it wasn't as cool as this one ;) ) If you have any questions etc. throw them in the comments.  Enjoy!

Web Developer Training - jQuery

Print This Post Print This Post