This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Find Location Of .js File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Find Location Of .js File" Watch "Find Location Of .js File" New topic

Find Location Of .js File

James Ellis
Ranch Hand

Joined: Oct 14, 2004
Posts: 205
I have some javascript code on one webapp that is being included on an html in another webapp.

Server: ServerOne
Filename: includeme.js

Server: ServerTwo
Filename: testpage.html

The page is rendered when a user types the following into a browser:

which includes the contents of "includeme.js".

I am wondering if it is possible from the javascript inside "includeme.js" to parse the URL that was used to include it and extract the word "ServerOne" from the URL.

I know this is a strange requirement, and there are a lot of workarounds but I am wondering if the javascript can get ahold of the URL used to include it.

Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
not sure if it would work, but you might be able to give the script tag an id and use document.getElementById("foo") and read the src and see if it gives you the info you need. Not positive it will work.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

That type of implicit branching is really icky and can only lead to trouble. If you need to exhibit different behaviors, why not pass a parameter that tells the code how to behave?

[Asking smart questions] [About Bear] [Books by Bear]
James Ellis
Ranch Hand

Joined: Oct 14, 2004
Posts: 205
I was able to give the script tag an id <script id="scriptTag" src=""> then grab the src attribute of the script tag via:

and then parse the string from there.

Thanks Everyone!
I agree. Here's the link:
subject: Find Location Of .js File
It's not a secret anymore!