dhtml a écrit :
SAM wrote:
>laredotornado a écrit :
>>>
If I have this include on my HTML page
<script type="text/javascript" src="scripts/myScript.js"></script>
How, from within "myScript.js" can I refer to the path of myself? In
other words, I would like to store in a variable the path WEBROOT/
scripts/myScript.js.
var myPath = document.getElementsByTagName('script')[0].src;
Maybe:
with in first script's tag of my tested file
src="test.js"
and file 'test.js' with :
alert(document.getElementsByTagName('script')[0].src);
that works (in my Firefox) as attempted
var scripts = document.getElementsByTagName('script'),
thisScript = scripts[scripts.length-1].src || location.href;
But that would only work while the script was loading.
of corse !
it's why it has to be in 1st position
It would not work if the script inserted other scripts before that line
of code. It would not be reliable in the jquery.ready function, or
window.onload or any other function that was called after the script had
loaded.
??
window.onload = function() {
alert(document.getElementsByTagName('script')[0].src);
}
in next script of my tested file works too
and it does too with in the body :
<a href="javascript
:alert(document.getElementsByTagNa me('script')[0].src)">
path</a>
Why do you want to do this?
Yes ! what to do with a such info ?
--
sm