View Full Version : 2 Unanswered Questions. Please help...

03-11-2005, 10:49 AM
1. How to know self path of the script inside js file ?
2. How to find existence of another js file using javascript?

In detail :

1. Say, you have http://www.mytest.com/index.html and loaded "somepathtojsfile/script.js" inside it.
Now inside script.js file, I want to know its self path ie, "somepathtojsfile/" using pure javascript.

2. How to know if a file "somepathtofile/somefile.ext" exists or not using javascript. I need this to test if some other js file really exist or not so that I can load it dynamically.

Hope some body helps out...

Thanking you in advance,


03-11-2005, 12:46 PM
Hi All,

I have found the solution for my first question my self. Here is the code to get self relative path for a javascript file inside itself. Thought that it will be useful for somebody.

function getRelativeScriptPath(fname)
var scripts=document.getElementsByTagName("script");
for(var i=0; i<scripts.length; i++)
if(scripts[i].src!=null && scripts[i].src.indexOf(fname)!=-1)
var scriptsrc=scripts[i].src.replace(/ /g, ''); //remove any spaces
if(scriptsrc==fname) return '';
return (scripts[i].src.replace(/\/[^\/]*$/, "")+'/');
return '';

function initMyScript() {


My second question is still unanswered... It will be great if somebody can help me out...


03-11-2005, 03:02 PM
For reference, this thread continues across multiple forums...

03-11-2005 03:53 AM (http://www.webdeveloper.com/forum/showthread.php?s=&threadid=59273)
03-11-2005, 03:54 AM (http://www.sitepoint.com/forums/showthread.php?t=242166)
03-11-2005, 04:07 AM (http://forums.devshed.com/t235218/s.html)

03-11-2005, 03:28 PM
hey afru, welcome to Coding Forums ...

presumably, posting here was why you weren't too concerned about returning to DevShed to post any gratitude for help received, eh?!

of course, that's why we all get cheesed off, because we'd be helping you at the same time as someone else, but you'd never tell anyone that you had received or solved the problem for yourself.

in short, never cross-post within forums or across other ones, because it wastes everyone's time. kapiche? :)

03-11-2005, 08:04 PM
but you'd never tell anyone that you had received or solved the problem for yourself.

Hi All,

I think I am guilty by cross posting this issue on diffrent forums. Basically, I am newbie to forums and thought that it only helps more people to know about the coding. But I am upset with jbot's saying that I dont tell anyone that I have solved the problem. As far as I am concerned, I have posted the answer to my 1st question on all the forums as soon as I got it. That too within hours of my first post so that no one other will waste his time on it...

And I am sure that it will help out hundreds of new javascript programmers who will get into trouble while including relative Path scripts. May be I should be a little more clear about its usage.

Lets say you have these files.


Now if you use those interfacepics in your js file and include the js file in index.html file it will work fine.

what if you want to move your html file may be to some other directory. Then you will include js file path like src="somedir/someotherdir/mainscript.js".

Now the js file is relative to the index.html but it runs in the same directory as that of index.html. So you have to include use interface pics as somedir/someotherdir/interfacepic1.jpg and somedir/someotherdir/interfacepic2.jpg
But js file dont know its own path from the html file.

Thats where my code will come into help. If you can get the relative path into some var relpath; then the interfacepic's path will be relpath+'interfacepic1.jpg';

Now you can move index.html file anywhere on your site and call the js file with pefection. I am sure lots of future projects will use this code to get the relative path. If you have any idea about the famous htmlarea, they need to create global variable to store editor scripts path, so that the script can know its own path. Using our code it can be easily achieved.

Anyhow coming to cross-posting again,

I have very strong reasons for cross-posting.

1. Any programmer who gets into real trouble will be in hurry to get out of it as soon as possible(Before his boss tosses him out). So he will find to get as many ways as he can to solve it. Cross-posting helps him to solve the problem soon and he will help others with the same pleasure.

2. If a topic is posted on multiple forums, the chances of viewing that post gets more. So if the topic is really useful, then hundreds of programmers will benifit from it. If it is posted only on this forum, you can think over how many other newbies who visit other forums are going to worry about the same problem.

Taking this own topic into consideration, I dont think anyone can show me an answer for the above problem on any other resource. I have searched for more than a day but didnt able to find it. Then only I posted it. And I dont want hundreds of other people to search and search like me and waste their time for the problem just because they have not visited this forum. Thats why I cross-posted my answer as well on all forums.

And from the perspective of helpers, they should never feel bad about cross posting because it only helps them to know the answer when they are unable to answer it on thier forum. They should remember they are also learning something by answering to other's posts.

But I too have some strict rules about cross posting.
1.No cross-posting within the same forum.
2.All cross-posts should have the same title so that the helpers can just avoid opening the topic when they come across it on multiple forums. And I am sure I have titled my posts on all forums one and the same.

Conclusion : Cross-Posting is not insane, It only helps hundreds (may be thousands) of people to know the answers without wasting their valuable programming time. But care should be taken that you shouldn't make helpers feel bad about wasting their time reading the same post with different titles on different forums. If the title is same, it is really encourageable to share your code on different forums so that some more people can get it.

And if you really think I should be banned for sharing my code on multiple forums even now, I will feel guilty and never share my code again....

Thanking you,