/    Sign up×
Community /Pin to ProfileBookmark

managing image resources: where to put them so JS can always find them?

I’m trying to write some widgets of my own in Javascript. It occurs to me that if I want images to be part of my widgets (like a ‘busy’ icon) then I need to put those images somewhere. Given that I want to use my widgets on any of the websites I work on or on any particular page no matter where it might be located, I am now wondering where to put these files?

Does anyone have a recommendation about where to put image files like this and how to make sure that Javascript knows where to find them? FYI, I’m not a big fan of just using absolute docroot paths (e.g., ‘/images/busy.gif’) because a) I don’t always have the luxury of dedicated development server and b) I might want different skins for different regions of a particular site.

Any input would be much appreciated.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 04.2008 — I use relative urls in html, but I always use absolute urls in javascript. You never know where you are going to call a script from, but you do know where the resources are.
Copy linkTweet thisAlerts:
@sneakyimpauthorMar 04.2008 — Isn't that a bit of a nightmare to maintain if you move something? Or suppose you want to use the JS on an entirely different site?
Copy linkTweet thisAlerts:
@mrhooMar 04.2008 — That is why I use absolute urls.

Whenever I write a src or href or data attribute, or apply a background image url, I transform the pathname with a preface for the host and any level of the directory structure I'm located at.

image.src= Run.pathTrack('/art/clips/clip12.gif') will come out with the current host prefaced to the path. If I start the url with 'http://' it will get used as is.

It is not difficult, if you make a practice of converting every resource before you appy it.

I use these two methods in my scripts- the first is only used once, when a page opens, and the second is used whenever I set a file path

[CODE]findRoot:function(rootStr){
var L= location.href;
L= L.replace(/\/g,'/');
var ax= rootStr? L.indexOf(rootStr): -1;
ax= (ax== -1)? L.indexOf(location.pathname)+ 1: ax+ rootStr.length;
return L.substring(0,ax);
}[/CODE]


// Memry is a global, Memry.Rte uses the code above to set the protocol and host, (and a directory path in subdomains) as a preface to local urls.

[CODE]pathTrack:function(url){
url= url.replace(/\/g,'/');
if(url.indexOf(location.protocol)!=-1 || /^(mailto|http):/i.test(url)) return url;
if(/^[!/]/.test(url)){
return Memry.Rte + url.substring(1);
}
var Loc= location.href.replace(/\/g,'/');
var ax= Loc.lastIndexOf('/');
Loc= Loc.substring(0,ax);
var L= Memry.Rte.length;
while (/^..//.test(url)){
if(ax< L) return '';
ax= Loc.lastIndexOf('/');
Loc= Loc.substring(0, ax);
url= url.substring(3);
}
return Loc + '/' + url;
}[/CODE]
×

Success!

Help @sneakyimp 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...