/    Sign up×
Community /Pin to ProfileBookmark

Get URL of .js file

Hi,
My html page calls a js file containing my js functions.
I need to insert the URL of the js file (in each installation it is different) into a var.

I tried

[CODE]var thispage=location.href;[/CODE]

but this gives the URL of the calling html page, not of the js ile.

Any ideas?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Nov 20.2010 — Provided this is not called by a function, it'll work.var scripts = document.getElementsByTagName("script");
var thispage = scripts[scripts.length-1].src;
Copy linkTweet thisAlerts:
@svidgenNov 20.2010 — I'd be interested to learn what this is needed for.
Copy linkTweet thisAlerts:
@bradmcauthorNov 21.2010 — I'd be interested to learn what this is needed for.[/QUOTE]
Simple - I want users of my js code to get a different behavior depending on the time elapsed since they uploaded the code.

The first few days they get basic stuff. If they persist they get more content. Long time users get premium content.

Sure, it's easy to get around - but my users aren't coders. It'll work.
Copy linkTweet thisAlerts:
@svidgenNov 22.2010 — Simple - I want users of my js code to get a different behavior depending on the time elapsed since they uploaded the code.

The first few days they get basic stuff. If they persist they get more content. Long time users get premium content.

Sure, it's easy to get around - but my users aren't coders. It'll work.[/QUOTE]


I don't understand. How does knowing the URL of a script from within the script facilitate a gradual release of content?
Copy linkTweet thisAlerts:
@bradmcauthorNov 22.2010 — I don't understand. How does knowing the URL of a script from within the script facilitate a gradual release of content?[/QUOTE]
Knowing the URL enables me to check the modification date of the script file. Then I release more and more content, according to the age of the file.

I can't use the date of the html webpage, as the users constantly update their pages - not good. But the js gets installed once so file age usually persists.

Basic - but works.
Copy linkTweet thisAlerts:
@svidgenNov 22.2010 — Basic - but works.[/QUOTE]

It sounds very un-basic to me. Why isn't the check just performed server-side? Perhaps I'm still not understanding what's going on here ...
Copy linkTweet thisAlerts:
@bradmcauthorNov 22.2010 — The check is performed via window.XMLHttpRequest() and getResponseHeader('Last-Modified').

If you have an easier way to find the modification date of the js file - that would help.

But nevermind - everything's working right now. Life's good.
Copy linkTweet thisAlerts:
@svidgenNov 22.2010 — Hmm ... well, alright. I'm just not certain why you're not checking the timestamp at the time the file is served up, serving content conditionally at that point -- different access keys or whatever.

But, I don't mean to drag you off into a full review of your project if you have a solution that meets your needs. I was curious. My curiosity has been entertained. No need to entertain it more unless you're hoping for some criticism ?
×

Success!

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