1

I want to do something like this:

<img src="" class="after-all" data="/assets/huge-bg.png" /> <div class="after-all" data="/actions/div-content.html"></div> 

And in JS

$("after-all").loadMissing(); 

I think the best way is build a plugin with recognize method:

if (img) $(this).attr('src', $(this).attr('data')); if (div) $(this).load($(this).attr('data')); 

Is this possible ? What is the best way to do this ? How to do this ?

2
  • 1
    Images are loaded asynchronously anyway. Commented Jan 29, 2013 at 14:55
  • You may be interested in the preloadjs library of createjs Commented Jan 29, 2013 at 14:58

1 Answer 1

1

You can use is():

$.fn.loadMissing = function() { return this.each(function() { var $this = $(this); if ($this.is("img")) { $this.attr("src", $this.attr("data")); } else if ($this.is("div")) { $this.load($this.attr("data")); } }); }; 

Note that you should use another attribute than data to store this information. An HTML5 data attribute like data-content might be a better solution.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.