-1

I have no experience in javascript. Basically i want to get image src and aria-label name using javascript. I try many scripts on console like this

var image = document.getElementsByClassName("");
var src = image.getAttribute('src');
console.log(src);

but did't get solution. Can any one please tell me how i can get this.

enter image description here

URl is that i want to get using javascript. Thanks

5
  • Why there isn't any classes specified?You should use img profpic class. Commented Sep 27, 2016 at 12:17
  • getElementsByClassName returns HTMLCollection, not element... Commented Sep 27, 2016 at 12:18
  • This is facebook home page. I want to get facebook profile pic and name using javascript. I'v need to use it in my project. Commented Sep 27, 2016 at 12:19
  • There is no image tag showing in chrome inspector. Are you displaying image as background of i tag? Commented Sep 27, 2016 at 12:20
  • @ Eldo.Ob I know but i did't found any id here :( Commented Sep 27, 2016 at 12:20

2 Answers 2

1

Your image is not in src attribute. It is in background-image.

    var image = document.getElementsByClassName("profpic")[0];
    var image= image.css('background-image');
    var src = image.replace('url(','').replace(')','').replace(/\"/gi, "");
    alert(src);
Sign up to request clarification or add additional context in comments.

2 Comments

Error VM347:2 Uncaught TypeError: image.css is not a function(…)(anonymous function) @ VM347:2
Because .css() is a jQuery function reference so you should include jQuery in your script.
-1
var elem=document.getElementsByClassName("img profpic")[0];
var match=elem.style.backgroundImage.match(/url\(\"(.*?)\"\)/)

var aria_label=elem.getAttribute("aria-label");
var background_url=match[1];

1 Comment

Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.