/    Sign up×
Community /Pin to ProfileBookmark

reference URL and swap image?

Is it possible to reference a given URL with a line of JavaScript, and load a particular image into a given location?

For example: if URL=”www.test.com/welcome.htm” then imageA=”img1.jpg” if URL=”www.test.com/about.htm” then imageA=”img2.jpg” else imageA=”img3.jpg”

This is for a template-driven ASP website (i can’t swap the images manually, it’s the header image)… any help is greatly appreciated.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 14.2010 — Sets the image to the same name as the url

URL="www.test.com/welcome.htm" then image="welcome.jpg"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">
window.onload=function() {
var fileName = location.pathname.substring(location.pathname.lastIndexOf('/')+1).split('.');
document.getElementById('header').setAttribute('src', fileName[0]+'.jpg');
};
</script>

<style type="text/css">
* {margin:0;padding:0;}
</style>

</head>
<body>
<img id="header" alt="" src="default.jpg" height="100" width="500">
</body>
</html>
×

Success!

Help @Stratt134 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.25,
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,
)...