/    Sign up×
Community /Pin to ProfileBookmark

Can I use If/Else statements in a return?

Good morning. Hope you all had an amazing weekend.

[CODE]function() {
var element = document.getElementsByClassName(“MagicZoomPlus”)[0];
var image = “https://www.mywebsite.com” + element.getElementsByTagName(‘img’)[0].getAttribute(“src”);
return image;
}
[/CODE]

That is my current code, which is working fine except some images are stored locally while others are on a CDN with a different path. Can, and if so how, would I write it to say something like

[CODE]If img src starts with “/uploads” than
var image = “https://www.mywebsite.com” + element.getElementsByTagName(‘img’)[0].getAttribute(“src”);
Else than
var image = element.getElementsByTagName(‘img’)[0].getAttribute(“src”);
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Matty2ShoesauthorMar 27.2017 — Example of current code. On some pages it returns a valid url as the "img src" it's pulling is "/uploads/*/*" and others it's an incorrect url as it returns mywebsite.comhttps:cdn-urlstring
Copy linkTweet thisAlerts:
@rootMar 27.2017 — Just test for the instance of [B]uploads[/B] in the URL and if it is not present, add it in.
×

Success!

Help @Matty2Shoes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...