<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>!DOCTYPE &#187; javascript</title>
	<atom:link href="http://www.createsoft.co.uk/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.createsoft.co.uk/blog</link>
	<description>Software Development and Web Design</description>
	<lastBuildDate>Mon, 31 May 2010 09:36:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JavaScript Tip: Identify the location of a JavaScript file</title>
		<link>http://www.createsoft.co.uk/blog/2010/05/javascript-tip-identify-the-location-of-a-javascript-file/</link>
		<comments>http://www.createsoft.co.uk/blog/2010/05/javascript-tip-identify-the-location-of-a-javascript-file/#comments</comments>
		<pubDate>Mon, 03 May 2010 13:56:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.createsoft.co.uk/blog/?p=38</guid>
		<description><![CDATA[This is a very short JavaScript tip.
Occasionally it is useful to obtain the path of a JavaScript file; for example, in order to refer to an image or other file which is located relative to your file.
Updated.

Suppose you have built a JQuery plugin or other JavaScript file, which requires a &#8216;loading&#8217; animation; you might set [...]]]></description>
			<content:encoded><![CDATA[<p>This is a very short JavaScript tip.</p>
<p>Occasionally it is useful to obtain the path of a JavaScript file; for example, in order to refer to an image or other file which is located <em>relative</em> to your file.</p>
<p><em>Updated.</em></p>
<p><span id="more-38"></span></p>
<p>Suppose you have built a JQuery plugin or other JavaScript file, which requires a &#8216;loading&#8217; animation; you might set a directory structure such as,</p>
<ul>
<li>js/myfile.js</li>
<li>js/loading.gif</li>
</ul>
<p>In order to locate the loading GIF you need to know the exact location of the JavaScript file, &#8216;myfile.js&#8217;</p>
<p>To do this, add the following to the top of the JavaScript file;</p>
<pre>    var jsPath= document.scripts[document.scripts.length-1].src;
    if(jsPath.lastIndexOf('/')!=-1) {
        jsPath = jsPath.substring(0, jsPath.lastIndexOf('/')+1);
    }</pre>
<p>This works because at the moment a script is loaded, it is the last script to appear in the document.scripts array.</p>
<p>Later, you can use the path, in this example to reference an image;</p>
<pre>    var imgPath = jsPath+"/loading.gif";</pre>
<p>Update:</p>
<p>I discovered that this does not work in Firefox, though it works fine in other browsers.</p>
<p>To get around it, I used a jQuery construct instead.</p>
<pre>var jsPath = '';</pre>
<pre>if(typeof document.scripts != 'undefined') {</pre>
<pre>    jsPath = document.scripts[document.scripts.length-1].src;</pre>
<pre>} else {</pre>
<pre><strong>    var srcArr = $('head script');</strong></pre>
<pre><strong>    jsPath = srcArr[srcArr.length-1].src;</strong></pre>
<pre>}</pre>
<pre>I'd have preferred a non-jQuery solution, will add that in when I can!</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.createsoft.co.uk/blog/2010/05/javascript-tip-identify-the-location-of-a-javascript-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

