aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes jquery - how to make loading images on demand better Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "jquery - how to make loading images on demand better" Watch "jquery - how to make loading images on demand better" New topic
Author

jquery - how to make loading images on demand better

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26216
    
  66

This code does work. It is called inside a conditional to ensure it only happens once. It goes through the emoticons urls (we have a url in the panel to the help so that one doesn't match) and creates/loads an image adding it to the panel.



My doubts are
1) This feels unreadable. It may because I rarely use a lot of jQuery in a small space. Or it is like how reg exps are unreadable without a ton of effort.
2) I have a suspicion that something inefficient is going on. I don't think I'm doing any extra lookups. The only thing I can think of is extra page re-arranging. Not sure of the tradeoff between repainting the panel and not seeing anything until it is done. I went with the former because it is only a problem for people with slow bandwidth. Which is when seeing something happening helps.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15357
    
    6
Well your code takes one of the fastest seector and brings it to a halt. NEVER use id as an attribute lookup.


BAD BAD BAD

BAD BAD


GOOD


Why are you looking for the data-url to have http? That is also being overly protective. Just check to see if the attribute is there



Other option is to not rely on the data attribute, but add a class and use the class selector.



Now stop mixing DOM and jQuery, pick one. Also jQuery has data(), no need to use an attribute look up.




Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26216
    
  66

Thanks Eric! Nothing like having your code ripped apart in public getting an awesome code review and learning a lot.

Eric Pascarello wrote:Well your code takes one of the fastest seector and brings it to a halt. NEVER use id as an attribute lookup.

Good point. I didn't think about it because I added my code under a couple lines under $("img[id$=emoticon_toggle_image]").click(...). And yes "emoticon_toggle_image" is the name, not the ending. I've fixed this too.

Eric Pascarello wrote:Why are you looking for the data-url to have http?

Because I didn't know you could just check for the presence of an attribute. I thought about using a class, but I still need a way of passing in the URL to load for each image.

Eric Pascarello wrote:Now stop mixing DOM and jQuery, pick one. Also jQuery has data(), no need to use an attribute look up.

I pick jQuery . Didn't know about data(). That's nice! I did know that $() was expensive, but I falsely assumed $(this) wouldn't be because it was "this".

Oddly data() is returning undefined. The second link works, the first does not.


I think I need to re-read Bear's book. I am clearly ready for understanding the basics better over piecing together working code. It used to be a struggle to piece together working code so I wasn't ready then.

Seriously though. I've fixed the code and learned a lot from this post. Thanks again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56229
    
  13

In general, while selection is comparatively expensive, it's highly optimized and I generally don't worry too much about performance until it actually proves to be a problem, or I'm selecting inside a tight loop. Generally, simpler selectors are faster (but of course, there are always corner cases.)

I usually try to use the simplest selector that makes reading the code easy, and adjust if necessary.

Selecting the same thing over and over again is to be avoided, not only because it takes extra time, but , like in other code, if somethings in two or more places, it's a bear to maintain. So if you're going to need a wrapped set to hang around for multiple references, store it in a variable:

var allImages$ = $('img');

The $ in the variable name is a hint that the variable references a jQuery wrapped set.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: jquery - how to make loading images on demand better
 
Similar Threads
Need help with Jquery
escape function in jquery
How do I parse this using JDOM?
Writing custom JQuery Selectors
Question .html() function