<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Dave Arlin - Bloggin' - Deployment</title>
    <link>http://blog.davearlin.com/</link>
    <description>learn forever</description>
    <language>en-us</language>
    <copyright>Dave Arlin</copyright>
    <lastBuildDate>Mon, 03 May 2010 14:54:25 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>dave@davearlin.com</managingEditor>
    <webMaster>dave@davearlin.com</webMaster>
    <item>
      <trackback:ping>http://blog.davearlin.com/Trackback.aspx?guid=915a5fb7-6059-43dd-ace5-635e12347ebb</trackback:ping>
      <pingback:server>http://blog.davearlin.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.davearlin.com/PermaLink,guid,915a5fb7-6059-43dd-ace5-635e12347ebb.aspx</pingback:target>
      <dc:creator>Dave Arlin</dc:creator>
      <wfw:comment>http://blog.davearlin.com/CommentView,guid,915a5fb7-6059-43dd-ace5-635e12347ebb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.davearlin.com/SyndicationService.asmx/GetEntryCommentsRss?guid=915a5fb7-6059-43dd-ace5-635e12347ebb</wfw:commentRss>
      <title>Simple Tip for Accessing Relative Path Items in Javascript in your ASP.NET Website</title>
      <guid isPermaLink="false">http://blog.davearlin.com/PermaLink,guid,915a5fb7-6059-43dd-ace5-635e12347ebb.aspx</guid>
      <link>http://blog.davearlin.com/2010/05/03/SimpleTipForAccessingRelativePathItemsInJavascriptInYourASPNETWebsite.aspx</link>
      <pubDate>Mon, 03 May 2010 14:54:25 GMT</pubDate>
      <description>&lt;p&gt;
One common problem I’ve faced when developing applications is when I need to access
an Image file or some other artifact that is relative to my website.
&lt;/p&gt;
&lt;p&gt;
I’ve often gotten “lucky” where my virtual directory path locally matches my QA, Staging,
Production, etc. So I’m able to have javascript code like this:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0062998b-391c-47d2-92a9-d6cd0190ff53" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
$(&lt;span style="color:#a31515"&gt;'.ok-button'&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;, &lt;span style="color:#a31515"&gt;'/Content/Images/btn_ok.jpg'&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
$(&lt;span style="color:#a31515"&gt;'.ok-button'&lt;/span&gt;).hover(&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;function&lt;/span&gt;() { $(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;, &lt;span style="color:#a31515"&gt;'/Content/Images/btn_okOver.jpg'&lt;/span&gt;);
}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
, &lt;span style="color:#0000ff"&gt;function&lt;/span&gt;() { $(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;, &lt;span style="color:#a31515"&gt;'/Content/Images/btn_ok.jpg'&lt;/span&gt;);
}&lt;/li&gt;
&lt;li&gt;
);&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Here, I’m assuming that my images will always be stored somehow like this:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://localhost/Content/Images"&gt;http://localhost/Content/Images&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://[test-dns]/Content/Images"&gt;http://[test-dns]/Content/Images&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://[prod-dns]/Content/Images"&gt;http://[prod-dns]/Content/Images&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
etc…
&lt;/p&gt;
&lt;p&gt;
That above javascript will fail if I am forced to be put into a Virtual Directory
such as this:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://[prod-dns]/[some-virtual-directory]/Content/Images"&gt;http://[prod-dns]/[some-virtual-directory]/Content/Images&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The most effective way I’ve found to get around this is by creating a hidden field
on my main master page that contains the value derived by the web server of the root
URL to my website. This root URL might contain no virtual directory or 10 virtual
directories depending on the IIS Web Server set up.
&lt;/p&gt;
&lt;p&gt;
I’m using ASP.NET MVC, but this example can easily be ported into a Page_Load event
with standard ASP.NET web forms.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:52f40903-a23d-4f2e-87fc-e84a8355e13c" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt;
&lt;li&gt;
&lt;span style="background:#ffee62"&gt;&lt;%&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; virtualPath = &lt;span style="color:#2b91af"&gt;HttpRuntime&lt;/span&gt;.AppDomainAppVirtualPath;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       virtualPath = virtualPath.EndsWith(&lt;span style="color:#a31515"&gt;"/"&lt;/span&gt;) ? virtualPath : virtualPath + &lt;span style="color:#a31515"&gt;"/"&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffee62"&gt;%&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;&lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;input&lt;/span&gt; &lt;span style="color:#ff0000"&gt;id&lt;/span&gt; &lt;span style="color:#0000ff"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff"&gt;"website-image-path"&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt; &lt;span style="color:#0000ff"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff"&gt;"hidden"&lt;/span&gt; &lt;span style="color:#ff0000"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff"&gt;'&lt;/span&gt;&lt;span style="background:#ffee62"&gt;&lt;%&lt;/span&gt;=virtualPath + "Content/Images/" &lt;span style="background:#ffee62"&gt;%&gt;&lt;/span&gt;&lt;span style="color:#0000ff"&gt;'&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&gt;&lt;/span&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Just place that code somewhere in the body of your master page.
&lt;/p&gt;
&lt;p&gt;
Then access it via JQuery, or standard javascript using something like this:
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a6c111d5-273d-4979-a992-ad2e0a7295b7" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
$(&lt;span style="color:#a31515"&gt;'#website-image-path'&lt;/span&gt;).val();&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Then finally you’re javascript might look something like this to get at your actual
Image source.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0e8449dc-9e89-4ec4-8e2c-2be80e91fffe" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"&gt;
&lt;li&gt;
&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; websiteImagePath = $(&lt;span style="color:#a31515"&gt;'#website-image-path'&lt;/span&gt;).val();&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
$(&lt;span style="color:#a31515"&gt;'.ok-button'&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;,
websiteImagePath + &lt;span style="color:#a31515"&gt;'btn_ok.jpg'&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;
$(&lt;span style="color:#a31515"&gt;'.ok-button'&lt;/span&gt;).hover(&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
&lt;span style="color:#0000ff"&gt;function&lt;/span&gt;() { $(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;,
websiteImagePath + &lt;span style="color:#a31515"&gt;'btn_okOver.jpg'&lt;/span&gt;); }&lt;/li&gt;
&lt;li&gt;
, &lt;span style="color:#0000ff"&gt;function&lt;/span&gt;() { $(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;).attr(&lt;span style="color:#a31515"&gt;'src'&lt;/span&gt;,
websiteImagePath + &lt;span style="color:#a31515"&gt;'btn_ok.jpg'&lt;/span&gt;); }&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;
);&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://blog.davearlin.com/aggbug.ashx?id=915a5fb7-6059-43dd-ace5-635e12347ebb" /&gt;</description>
      <comments>http://blog.davearlin.com/CommentView,guid,915a5fb7-6059-43dd-ace5-635e12347ebb.aspx</comments>
      <category>ASP.Net MVC</category>
      <category>Deployment</category>
      <category>JQuery</category>
    </item>
  </channel>
</rss>