Javascript not found in my ASP.Net Application - javascript

I am having a problem in the location of my javascript, the location is right, but when I run it in my visual web developer 2010 express, the location can not be found, I don't know the reason why...
here is the location of my javascript:
<script src="Style/javascript/jquery-1.7.1.js" type="text/javascript"></script>
here is the error:
**Server Error in '/Maitenance' Application.**
**The resource cannot be found.**
**Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.**
**Requested URL: /Maitenance/Maintenance/Style/javascript/jquery-1.7.1.js**

Use this... It will work
<script src="<%=Page.ResolveUrl("~")%>Style/javascript/jquery-1.7.1.js" type="text/javascript"</script>

I guess you are using a master page and your .aspx page is put in another directory. The file path you included in master page is relative to the .aspx file. It works OK when your page in the same directory with the master page.
You can include your js file using ResolveUrl:
<script src="<%=ResolveUrl("~/js/jquery.js")%>" type="text/javascript"></script>
or you can include your script in the code behind of master page:
ClientScript.RegisterClientScriptInclude("jquery", ResolveClientUrl("~/js/jquery.js"));

Related

Load scripts in html-file from same subdirectory (Apache)

I feel like asking the most stupid question, but i searched for a while now and could not get it figured out.
I have the following file structure under my Apache DocumentRoot:
DocRoot
- page1
- ...
- webapp
- index.html
- somescript.js
with index.html having a script tag looking like
<script src="somescript.js" type="text/javascript">
How do i configure Apache to serve https://myhostname.com/webapp so that the script get's loaded correctly? Page1 should stay accessible under https://myhostname/page1.
The current behaviour is that somescript.js does not get found, because the request is https://myhostname.com/somescript.js.
I do NOT want to set up a Virtual Host for this or edit the html file (get's generated).
Are webapp and page1 located in var/www/html? If so, probably your JS file is searched in html folder.
You have, at least, 2 solutions:
<script src="/webapp/somescript.js" type="text/javascript">
Configure virtual hosts for webapp and page1. So, you'll be able to connect to that pages via webapp.myhostname.com and page1.myhostname.com subdomens, and <script src="somescript.js" type="text/javascript"> will work for both of them, searching in their folders

ASP.NET Javascript Not being loaded when ASP Page has URL Mapping

I'm trying to retrieve the ID of an employee and show them in my details page by retrieving the Employee ID from the URL using Page Mapping in ASP.NET:
RouteTable.Routes.MapPageRoute("", "employee/{id}", "~/details.aspx");
Such that the URL will be:
www.myexamplewebsite.com/employee/7937822353
The problem is, the Javascript files don't get loaded and the console is full of my javascript errors. I get a 404 error in my JS scrips as well. The page can't find any JS file when mapped. Why is this happening when I map the URL? This does not happen if the URL is www.myexamplewebsite.com/7937822353.
What am I doing wrong?
EDIT:
This is how my JS files are referenced:
<script src="js/chatbar/rightside.js"> </script>
The script is relative, and it's from the perspective of the client side URL. So when you change the page route to have the page appear to be served from a /employee subdirectory, the correct relative path to your scripts changes. So it's looking for the scripts at /employee/js/charbar/rightside.js. You could change those relative paths (to something like ../js/chartbar/rightside.js), but you might run into a problem later if you change the routing again.
So instead, it's best to make the reference application root relative.
<script src='<%= ResolveClientUrl("~/js/chatbar/rightside.js")%>'></script>

iis cant find a javascript file

I have a problem with my a javascript file im refrencing to a master page.
this is the code:
<head runat="server">
<type="text/javascript" src="../Jquery1.6_vsdoc/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
the jquery files work fine, but the main.js not.
when i open the aspex file on the web browser and do view source, and try to see the code on the main.js file iis show this message:
HTTP Error 404.0 - Not Found The resource you are looking for has been
removed, had its name changed, or is temporarily unavailable.
what do i do worng here?
some info on the file (if it will help):
he is the same directory of the master page
he is ony refreced in 1 master page.
im using visual studio 2012
(sorry for my english)
Use Page.ResolveUrl() in Master Page scenarios
so your reference should look like this
<type="text/javascript" src="<%= Page.ResolveUrl("~/Jquery1.6_vsdoc/jquery-1.7.1.min.js") %>"></script>
<script type="text/javascript" src="<%= Page.ResolveUrl("~/main.js") %>"></script>
This will ensure that the page is mapped correctly as the Child Page may not be in the same location as the Master Page

Can't access Javascript file

I'm using webform asp.net and C#. I linked the master page with javascript file like this
<script type="text/javascript" src="javascript/main.js"></script>
but when I'm going to inner pages inside folders.. I can't access the JavaScript file.. (404 error).I tried to solve the problem by using ResolveClientUrl but it's not work!
What's the problem?
but when I'm going to inner pages inside folders.. I can't access the JavaScript file
Since the page you're accessing is at a different level than your master page, you want to make the script path relative to the root of your application
<script type="text/javascript" src="/javascript/main.js"></script>
EDIT if that's not working, then I would keep the script tag as it is above, run it in Chrome with the developer tools / network tab open, and look at the exact address that shows up for the failed request. Then look closely at your application and see what's different / wrong.
You can do:
<script type="text/javascript" src="<%=Page.ResolveClientUrl("~/javascript/main.js") %>" />
Allows you to use ASP.NETs ~. If this doesn't work then the file doesn't exist in that directory.

asp.net unable to locate a resource file

Im attempting to utilize some custom script and css files within an asp page. In Visual Studio 2010 I am not getting any warnings or errors as to the status of these files, but when I attempt to run the page, and I open the javascript console I get the error:
Failed to load resource: the server responded with a status of 404 (Not Found)
Here's how I am attempting to load the files in my ascx file:
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.scripts.js"></script>
<script type="text/javascript" src="scripts/jquery.alerts.js"></script>
<link href="styles/jquery.alerts.css" rel="stylesheet"/>
Anyone know whats going on here, why the browser can't locate the files but visual studio can?
This happens when the location of the .aspx file is different to the location of the user control (ascx).
When the user control is rendered in the browser it will actually have the location of the .aspx and tries to make the reference from that point. Whereas in VS, it will try to make the reference to the file from the .ascx location.
Therefore, try referencing the files as if you were making the references from the .aspx file location. It shouldn't give you any error when rendering that page.
I've upvoted aleafonso's answer since he's correct. Now, to solve this issue, you can use the ResolveClientUrl method.
Something like this:
<script type="text/javascript" src='<%=ResolveClientUrl("scripts/jquery.js"%>'></script>

Categories