Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  170] [ 7]  / answers: 1 / hits: 27354  / 5 Years ago, thu, october 10, 2019, 12:00:00

I am writing a website where I load this link (https://source.unsplash.com/random/1920x1080/?wallpaper,landscape) to get a random photo. The photo behind the link changes periodically and when its loaded of the site I only see this link https://source.unsplash.com/random/1920x1080/?wallpaper,landscape do you think there is a way to get the real source like this: https://images.unsplash.com/photo-1458571037713-913d8b481dc6?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=1080&ixid=eyJhcHBfaWQiOjF9&ixlib=rb-1.2.1&q=80&w=1920. When I open up the chrome devtools I can see the source in the Application tab but how can i access them?


More From » html

 Answers
3

You can use axios library to fetch the image and get the response url :





let randomURL = 'https://source.unsplash.com/random/1920x1080/?wallpaper,landscape';

axios.get(randomURL).then( data => {
// the url of the random img
console.log(data.request.responseURL);
});

<script src=https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js integrity=sha256-S1J4GVHHDMiirir9qsXWc8ZWw74PHHafpsHp5PXtjTs= crossorigin=anonymous></script>





and with vanilla JS :





fetch(https://source.unsplash.com/random/1920x1080/?wallpaper,landscape).then( data => {
console.log(data.url);
});





and if you want to support old browsers :





request = new XMLHttpRequest();
request.open(GET, https://source.unsplash.com/random/1920x1080/?wallpaper,landscape, true);
request.send(null);
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
console.log(request.responseURL);
}
}
}




[#51582] Wednesday, October 2, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brennonr

Total Points: 124
Total Questions: 101
Total Answers: 101

Location: Estonia
Member since Wed, Jun 8, 2022
2 Years ago
;