Solved

How to get: Request.ApplicationPath & Request.Url.GetLeftPart

Posted on 2003-11-04
4
2,701 Views
Last Modified: 2012-06-27
I'm new to client-side Java scripting ... that said .... I use C# in CodeBehind, I get the ASP .NET application path using the following code.

   string mRootPath = Request.ApplicationPath;
   if (mRootPath.Substring(mRootPath.Length-1, 1) == "/")
   {
      mRootPath = mRootPath.Substring(1, mRootPath.Length-1);
   }
   string mPath = Request.Url.GetLeftPart(UriPartial.Authority) + mRootPath + "/";

Resulting in mPath equal to ...
   during development>  http://localhost/MyApp/
   once deployed>       http://MySite.com/

I need to get the same information on the Client-Side using Java Script.
Possible?
0
Comment
Question by:volking
  • 2
4 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 9679982
<script language=javascript>
  var jsRootPath = "<%= mRootPath %>";
</script>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 9679986
can you just do this?

<SCRIPT>
  var mPath = "<%=mPath%>";  // create a client string from the server string
</SCRIPT>
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 500 total points
ID: 9680012
or if you just want to get the path from javascript:

<script language=javascript>
   var jsRootPath = window.location;
   
   // strip everything after the last occurance of '/'
   jsRootPath = jsRootPath.substring(0, jsRootPath.lastIndexOf("/"));
</script>
0
 

Expert Comment

by:profit11
ID: 10282598
the accepted answer causes an error unless "href" is used:

var jsRootPath = window.location.href;
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question