0

I have develop an application using jQuery. This application is working fine on my development machine. After release this application on server, jQuery is not working due to path problem.

   <script language="javascript" type="text/javascript" src='<%=ResolveClientUrl("~/scripts/jQueryv2.0.3.js") %>'></script>
 <%-- <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/redmond/jquery-ui.css" />--%>
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" href="css/common.css" />
<link rel="stylesheet" type="text/css" href="css/uploadfile.css" />
<link rel="stylesheet" type="text/css" href="css/jquery.plupload.queue.css" />
<script type="text/javascript" src='<%=ResolveClientUrl("~/scripts/jquery-ui.js") %>'></script>
<script type="text/javascript" src='<%=ResolveClientUrl("~/scripts/grid.locale-en.js") %>'></script>
<script type="text/javascript" src='<%=ResolveClientUrl("~/scripts/jquery.jqGrid.min.js") %>'></script>
<script type="text/javascript" src='<%=ResolveClientUrl("~/scripts/common.js") %>'></script>
<script type="text/javascript" src='<%=ResolveClientUrl("~/scripts/jquery.uploadfile.min.js") %>'></script>
<script type="text/javascript" src='<%=ResolveClientUrl("~/scripts/ServiceCall.js") %>'></script>
<script type="text/javascript" src='<%=ResolveClientUrl("~/scripts/plupload.full.min.js") %>'></script>
<script type="text/javascript" src='<%=ResolveClientUrl("~/scripts/moxie.js") %>'></script>
<script type="text/javascript" src='<%=ResolveClientUrl("~/scripts/jquery.plupload.queue.js") %>'></script>

Please see the below screen shot.

enter image description here

my url is http://somedomain.com/supportticket/default.aspx

after viewing into detail view i found path is something different. Path in debugger : http://somedomain.com/scripts/jquery.x.x.js / http://somedomain.com/css/common.css and it is expected to be : http://somedomain.com/supportticket/scripts/jquery.x.x.js or http://somedomain.com/supportticket/css/common.css

same is happening with CSS as well.

Manish
  • 103
  • 3
  • 18

1 Answers1

1

why not write full path to the css / js folder like this

<script src="/path/to/scriptfolder/filename.js"></script>
<link rel="stylesheet" type="text/css" src="/path/to/cssfolder/filename.css"/>

where / in front means that the folder is in the root, or use

<script src="../script/filename.js"></script>
<link rel="stylesheet" type="text/css" src="../css/filename.css"/>

where ../ indicates that the browser should request the file from a directory one level above the current directory

Usage Example

example if your webpage is www.example.com and your directory is like this

-/
  -home/
  -contactus/
    -supportticket/
      -Default.aspx
      -script/
        -script.js
      -css/
        -style.css

and you call the script from www.example.com/contactus/supportticket/Default.aspx then you can either write it like this

Full Path :

<script type="text/javascript" src="/contactus/supportticket/script/script.js"></script>

or Relative Path to your current page :

<script type="text/javascript" src="../script/script.js></script>

which should be equal with

<script type="text/javascript" src="<%=ResolveClientUrl("~/script/script.js") %>"></script>
Kyojimaru
  • 2,694
  • 1
  • 14
  • 22
  • Why should I write full path like '/' or '../', i have to refer the script and css path from the root of virtual directory not from the root of IIS directory. Now i am completely confused on path. As per my knowledge, when we use '~/' it means it is the root folder of the application / virtual directory and not the IIS root folder. writing full path does not seems logical to me as the name of virtual directory can change, so I always need to pick the relative path from the root of virtual directory – Manish Jun 11 '14 at 06:22
  • The above script mention in servicecall.aspx file which is located in the root folder of virtual directory. Hence '~/' should consider the root folder of virtual directory so the path becomes /scripts or /css, but it is referring the root folder of IIS. – Manish Jun 11 '14 at 06:25
  • Please refer this link. This also shows the same thing. – Manish Jun 11 '14 at 06:27
  • http://stackoverflow.com/questions/735684/asp-net-path-to-use-to-reference-css-and-js – Manish Jun 11 '14 at 06:27
  • sorry, I don't really understand about the virtual directory path, because I usually put all my script and css file in the root of my application ( if my web is www.example.com then my script file will be in www.example.com/script/.. ) and if I don't mistaken from this link http://stackoverflow.com/questions/1874636/what-is-the-difference-between-resolveurl-and-resolveclienturl#answer-1874676 it will result in the same thing. Can you tell me more about your directory – Kyojimaru Jun 11 '14 at 06:39
  • did your folder look like this, for example the page that call the script is www.example.com/contactus/supportticket/Default.aspx and your script folder is placed at wwwroot/contactus/supportticket/script/scriptname.js then you can call it using src="../script/scriptname.js" which is relative to your current page – Kyojimaru Jun 11 '14 at 07:06
  • Hey Kyojimaru, Thanks for your support and time. I have resolved this issue. What I have understood the property "ResolveClientUrl" was correct. I found my mistake. It's actually a SPA based application. In default.aspx there are 3 divs(header,content & middle), Using web service I render corresponding page and set the rendered content in content div using jQuery. So the problem was here when I render page it convert all script and css to '../script and ../css' path in script and css, later div is converted to new html content and dive is located in default.aspx in root of virutal dir. – Manish Jun 12 '14 at 04:00
  • After successful render of respective page it was looking script in the path '../script or ../css'. This was the problem. Anyways thanks for your support and time that you have spent to resolve my problem. – Manish Jun 12 '14 at 04:01