Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Javascript / Client Side Development

Can a script determine its own location?


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

On May 21, 12:29 pm, Martin Larsen <martin+spamfree

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.

On May 21, 6:29 pm, Martin Larsen <martin+spamfree+lar@bigfoot.com>
wrote:

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().

--
Rob

On May 21, 1:37 pm, RobG <r@iinet.net.au> wrote:

> 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

On May 22, 8:25 am, marlar <marlar@gmail.com> wrote:

> On May 21, 1:37 pm, RobG <r@iinet.net.au> wrote:

> > 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?

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.

--
Rob

On May 22, 4:55 am, RobG <r@iinet.net.au> wrote:

> 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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc