Can a script determine its own location?

M

Martin Larsen

Hi,

I have a script for a CMS that preloads an image by setting the SRC
attribute. The problem is that the location of the image as seens from
the final HTML page depends on whether the CMS has search engine
friendly (SEF) url's urned on.

If it is not on, the location is just the normal absolute location of
the image, and it is then easy to fetch it.

However, if SEF is on, the location could be anything. Pre-loading the
image will then usually fail because the url deduced by the script would
be wrong.

But the image is located relatively to the script itself, so if the
script could just determine "where I am" it would be a piece of cake to
get the address of the image!

But is it possible for a script to determine its own location?

Regards,
Martin
 
V

VK

Hi,

I have a script for a CMS that preloads an image by setting the SRC
attribute. The problem is that the location of the image as seens from
the final HTML page depends on whether the CMS has search engine
friendly (SEF) url's urned on.

If it is not on, the location is just the normal absolute location of
the image, and it is then easy to fetch it.

However, if SEF is on, the location could be anything. Pre-loading the
image will then usually fail because the url deduced by the script would
be wrong.

But the image is located relatively to the script itself, so if the
script could just determine "where I am" it would be a piece of cake to
get the address of the image!

But is it possible for a script to determine its own location?

If it's a single script with absolute path in src attribute then maybe
var myURL = document.scripts[0].src;

I did not test it on practice.
 
R

RobG

Hi,

I have a script for a CMS that preloads an image by setting the SRC
attribute. The problem is that the location of the image as seens from
the final HTML page depends on whether the CMS has search engine
friendly (SEF) url's urned on.

If it is not on, the location is just the normal absolute location of
the image, and it is then easy to fetch it.

However, if SEF is on, the location could be anything. Pre-loading the
image will then usually fail because the url deduced by the script would
be wrong.

But the image is located relatively to the script itself, so if the
script could just determine "where I am" it would be a piece of cake to
get the address of the image!

But is it possible for a script to determine its own location?

Once the document is loaded, you can reliably get all the script
elements using getElementsByTagName. You can sift through them,
looking for an attribute value that suits. If you want valid HTML,
add a search string to the src attribute and look for that e.g.

<script src="blah.js?xx>...</script>

If you don't want to do that and validity isn't an issue, give it a
custom attribute and use getAttribute().
 
M

marlar

Once the document is loaded, you can reliably get all the script
elements using getElementsByTagName. You can sift through them,
looking for an attribute value that suits. If you want valid HTML,
add a search string to the src attribute and look for that e.g.

<script src="blah.js?xx>...</script>

getElementsByTagName works! Thanks :)

But I don't understand what I would need the search string for? Would
it not be enough to look at the script name itself?

Regards,
Martin
 
R

RobG

getElementsByTagName works! Thanks :)

But I don't understand what I would need the search string for? Would
it not be enough to look at the script name itself?

I guess so, but your script may not know what the filename is, or you
may want to change it to whatever you want and just have the server
add the search string when it builds the page. Also, you might have
more than one script with the same name but different path, and so
on. I just figured it might give more flexibility.
 
M

marlar

Also, you might have
more than one script with the same name but different path, and so
on. I just figured it might give more flexibility.

You are right about this. The search string would make it safer.

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top