Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2816
  • Last Modified:

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

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
volking
Asked:
volking
  • 2
1 Solution
 
bobbit31Commented:
<script language=javascript>
  var jsRootPath = "<%= mRootPath %>";
</script>
0
 
knightEknightCommented:
can you just do this?

<SCRIPT>
  var mPath = "<%=mPath%>";  // create a client string from the server string
</SCRIPT>
0
 
bobbit31Commented:
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
 
profit11Commented:
the accepted answer causes an error unless "href" is used:

var jsRootPath = window.location.href;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now