-1

I am trying to replace all tags through a bookmarklet. I am ONLY using javascript. I've tried using the methods used in this link but that doesn't seem to work.

<script>
var imgs = document.getElementsByTagName("img");
for(var i=0, l=imgs.length; i<l; i++) {
imgs[i].src = "http://3.bp.blogspot.com/-KL7d7LdSANg/Tm5VLQf9k4I/AAAAAAAAACo/cSV52JoD7vk/s1600/cat-wallpaper-34-713472.jpg";
}
</script>

Is the code I was trying out. What am I doing wrong?

3
  • 3
    This code is working fine. Maybe there is a problem in another part of your code. Fiddle: jsfiddle.net/SAVN5 Commented Oct 30, 2013 at 21:15
  • 2
    "doesn't seem to work" is not very descriptive. What are you seeing vs what do you expect to see? Commented Oct 30, 2013 at 21:16
  • 1
    how are you turning this into a bookmarklet? bookmarklets don't usually have <script> in them. Commented Oct 30, 2013 at 21:16

2 Answers 2

4

This would be the "bookmarklet" version of your script

javascript:var imgs = document.getElementsByTagName("img");for(var i=0, l=imgs.length;i<l;i++){imgs[i].src = "http://3.bp.blogspot.com/-KL7d7LdSANg/Tm5VLQf9k4I/AAAAAAAAACo/cSV52JoD7vk/s1600/cat-wallpaper-34-713472.jpg";}
Sign up to request clarification or add additional context in comments.

2 Comments

How can I apply .style.backgroundImage to this?
@user2879652 are you serious? See where it says .src? That can be whatever property you want to change, in your case .style.backgroundImage.
-2

Hold this code in function and call it after DOM is load. Maybe its help u.

2 Comments

"I am trying...through a bookmarklet"
Oh,sorry,man! Didn't see

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.