jQuery / javascript Resolve client URL

Posted on 2010-09-21
Medium Priority
Last Modified: 2013-11-08

In my jQuery script I want to load content using the $.post method.
In this method I point to a specific controller etc...
Instead of using the fixed path localhost/StockManamgent ...... I would like jQuery to be able to resolve the used url for the virtual directory in IIS.

$.post("http://localhost/StockManagement/Stock/LoadBrands/" + materialId, null, function (data)

Is this possible?

Question by:gerrie-govaerts
  • 3
  • 2
  • 2
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33723548
you can see the current URL using

let me know if i missed something

Author Comment

ID: 33723594
No I'm sorry I can't get enough information from the location object.

Here are the possible options: http://www.javascriptkit.com/jsref/location.shtml

I actually need the virtual directory of IIS, not the controller, action etc...

LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33723616
If the virtual directory is there in current URL, you can get it from location.href,
else you need to send it from the server side script.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

LVL 83

Accepted Solution

leakim971 earned 750 total points
ID: 33724658
>Instead of using the fixed path localhost/StockManamgent ...... I would like jQuery to be able to resolve the used url for the virtual directory in IIS.
>Is this possible?
Not with Javascript

You may use something like : var vdir = "<%= Server.MapPath("\\LoadBrands").Replace("\\","\\\\") %>"


Test page :

(create a new website named WebSite123)
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript">
        var vdir = "<%= Server.MapPath("\\WebSite123").Replace("\\","\\\\") %>";
    <form id="form1" runat="server">

Open in new window

LVL 83

Expert Comment

ID: 33726032
Did you tried the two dots : ../

For example : ../../../otherDirectory/blabla

Author Closing Comment

ID: 33732439
var virtualPath = "<%=HttpRuntime.AppDomainAppVirtualPath%>";

solved my problem, so thx for giving me a good hint !

LVL 83

Expert Comment

ID: 33733103
You're very welcome! Thanks for the points Gerrie!

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
The viewer will learn how to dynamically set the form action using jQuery.
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)

586 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