Is there any method by which I can get url inside <img> tag from a link.
Eg: When I give a link of website, like a website or facebook page, I want to get all urls inside all the img tags in that site --> <*img src="url">
Is there any method by which I can get url inside <img> tag from a link.
Eg: When I give a link of website, like a website or facebook page, I want to get all urls inside all the img tags in that site --> <*img src="url">
First send a request to a page, get the content and parse it to html. Then get all img tags by using getElementsByTagName method.
For example, if you want to get all img tags on https://jsonplaceholder.typicode.com, your code should be like below.
fetch('https://jsonplaceholder.typicode.com').then(function (response) {
// The API call was successful!
return response.text();
}).then(function (html) {
// Convert the HTML string into a document object
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
var imgTags = doc.getElementsByTagName("img");
console.log(imgTags);
var imgTagArr = [...imgTags];
imgTagArr.forEach(img => console.log(img.src)); //iterate the all img tag srcs
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
Example output:
Yup ,
in Html like <img class="pqr" url ="xxxxxxx-yyy"/>
in Js file:
const img =document.querySelector('.pqr')
img.src // <-- this will give the src of the image