Can't access Javascript file - javascript

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.

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

Refrer javascript inside html file?

I have below folder structure.
inside: webapp/WEB-INF/some.jsp i have
i have javascript file in the same webapp/WEB-INF/js/myform.js location
i referred it in some.jsp as below:
<script type="text/javascript" src="js/myform.js"></script>
But it is not finding javascript file. in viewsource i am getting below lines:
<title>Error 404 NOT_FOUND</title>
</head>
<body><h2>HTTP ERROR: 404</h2><pre>NOT_FOUND</pre>
<p>RequestURI=/js/myform.js</p><p><i><small>Powered by Jetty://</small></i></p><br/>
is my javascript link correct in jsp file?
Please correct me.
Thanks!
The WEB-INF folder is secure, meaning you can't access resources placed in it directly using a URL from the browser.
To work around it, place the JS under the webapp/js directory.
webapp/WEB-INF is not the root of your application, put your dir js in webapp rather than in webapp/WEB-INF

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

Javascript not found in my ASP.Net Application

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"));

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