All, If I add an inline expression in the aspx page like below.
<script type="text/javascript">
var notAcceptError='<%= lblMessage%>';//the actually value is "test message"
</script>
As we know. There should exist a variable named lblMessage is defined in the code-behind file of aspx.
in this way , everything is fine.
But If I move the above code into a js file which is linked by the same page.
the output of expression failed. seems the asp.net can't recognize this expression.
I can't understand why this doesn't work in the linked external js file. In my understanding. writing in the aspx or an external js is the same. thanks.
The asp.net inline expression only work in .aspx files. It will not work in .js file because .js files served as static files and are not parsed at runtime.
If you really want to use a variable value in code-bind file into the js you can do like this.
In your aspx file do this.
<script type="text/javascript">
var notAcceptError = '<%= lblMessage%>'; //the actually value is "test message"
</script>
<%-- please note the external file is referenced after the javascript variable --%>
<script type="text/javascript" src="myjsfile.js">
</script>
And inside your myjsfile.js use the variable like this
alert(notAcceptError);
Your understanding is wrong. ASPX files are parsed and evaluated, whereas JS files are served from the server without any parsing or evaluation.
Related
I want to create a new file type (for example .jsrender). Inside this file, there is some JavaScript code, but also some code nuggets like <%= txt1.ClientID %> that have to be rendered before it's outputted.
How can I achieve this functionality? Also, how can I reference it? Can I do the following?
<script type="text/javascript" src="scripts/myjs.jsrender" runat="server" />
Will it work?
Also, I am going to reference it from code-behind file like below:
ScriptManager.RegisterStartupScript(
this.Page,
typeof(Page),
"JQueryUITimepickerAddonAjaxminJs",
String.Format("<script src='{0}' type='text/javascript' defer='defer'></script>",
ResolveUrl("~/Scripts/timepicker_addon/jquery-ui-timepicker-addon.ajaxmin.jsrender")),
false);
Do you think it will be rendered before it's outputted?
I know that we can create HttpModules for IIS, but I don't know what to do for this kind of functinality.
Thanks!
You just need to map that file extension to the ASP.Net handler. See the post below for doing that. Also look at his recommendation to use a rewrite to to just change the file extension dynamically.
How do I process .asp extensions using the .Net handler?
My website is organized in the following folder structure
Root
Pages
CustomerManagement
DepartMentManagement
Script
UserControls
Now i have a user control inside userControl Folder. I have to use a external js file. So i use it this way
<script type="text/javascript" language="javascript" src="../Script/slider.js"></script>
My understanding is that since both are under root so i have used a single ..
Now what happens is that when this usercontrol is used inside DepartMentManagement it checks for script folder inside CustomerManagement as .. refers to one hierarchy above and script file is not found.Even using a ~ doesnot work.
I want to make this script path independent of the path where this control is used. I don't want to move script reference code to the page as control requires script mot page.Please tell me how to achieve this.
Try it like this
<script type="text/javascript" language="javascript" src="<%= ResolveUrl("~/Script/slider.js") %>" ></script>
I'm trying to add a script reference to jQuery in my master page so that it will work for any page. It currently looks like this
<script type="text/javascript" src="jquery.js"></script>
The problem is that the path is always relative to the executing aspx page so this will only work if the "jquery.js" file is located in the same folder. To make it work I have to change the line to:
<script type="text/javascript" src="../../jquery.js"></script>
This is obviously less than ideal because it will only work for pages that are two levels deep from the root folder. If I try the following, IIS throws an error about an unexpected character.
<script runat="server" type="text/javascript" src="~/jquery.js"></script>
Any ideas?
EDIT: I forgot to mention as well that the script MUST be in the head tag
The current top answer throws a "ASP.NET Ajax client-side framework failed to load." error when I add it to my master page. Its thrown from javascript and not the .Net compiler. If I move the ScriptManager to the head section where it should be I get a compile error about the ScriptManager needing to be inside a form tag.
The third answer throws a "Illegal characters in path." exception from the compiler
EDIT 2: When I add that line to my head tag I get this error from IIS.
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)
SOLVED: I took the edited response from the answer below and put it inside an asp:ContentPlaceHolder element
You could use a ScriptManager:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/jquery.js" />
</Scripts>
</asp:ScriptManager>
EDIT: If you absolutely need this in your <head> section, you could do something like:
<head>
<script type="text/javascript"
src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>
EDIT 2: According to the comments, if you are observing that
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)
you may need to change the above to use the data-binding syntax:
<head>
<script type="text/javascript"
src="<%# Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>
Try <%# instead of <%= in Master page under head section
<script type="text/javascript"
src="<%# ResolveUrl("~/YourScriptFolder/YourJQueryOrJavascript.js") %>">
</script>
Then in Code Behind of Master page under Page_Load Event
Page.Header.DataBind();
Now you are good to go with either jQuery and JavaScript as well as CSS just you need to change your path in ResolveUrl which file you want to handle CSS, JavaScript, jQuery.
If you're not going to us asp:ScriptManager or absolute paths then you can do it like this:
<script runat="server" type="text/javascript"
src='<%= Page.ResolveUrl("~/jquery.js") %>'></script>
I do not know whether you guys found the solution to your problem or not. I was facing the same problem and going nuts to figure out why do I get "jQuery is undefined" error on the plugins i use. I tried all the solutions i get from the internet but no luck at all.
But, suddenly something splash on my mind that may be the script files should be in order. So, I moved the jquery referece to first position and everything start working like charm.
Remember guys, if you're using any plugins with jquery, make sure you use the folloing order of setting reference to those fiels.
reference to the jquery library
reference to the other subsequent plug-in libraries and so on...
e.g.:
"script src="js/jquery-1.3.2.min.js" type="text/javascript"...
"script src="js/jqDnR.min.js" type="text/javascript"...
"script src="js/jquery.jqpopup.min.js" type="text/javascript"...
"script src="js/jquery.bgiframe.min.js" type="text/javascript"...
Always make sure you must put the jquery reference to first and then the subsequent libraries.
Hope, this solves your problem especially when you use with MasterPages. Its very strange that it works no matter what order you use when you don't use MasterPages but when you do, then it somehow requres the proper order.
Good luck and happy coding,
Vincent D'Souza
Look at How to Run a Root “/”. This should fix all your issues regarding unresolved .js file paths. You basically reconfigure the VS Dev server to run your application as localhost:port/ as opposed to the regular localhost:port/application name/ making name resolution work the same way as it does on IIS.
For absolute path of the file for any page use it following:
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
<script type="text/javascript" src="/full/path/to/jquery.js"></script>
If this script tag goes directly to the browser, then you unlikely can substitute your site's root there. At least not on the server. So you can:
Deploy site to the root of domain
name and use absolute paths
(simplest solution).
Insert this
link with server control.
Preprocess resulting HTML before
sending it to the client (with
HttpResponse.Filter).
You can also use <base> HTML tag:
<base href="http://www.domain.com"></base>
and then all the links in header section are relative to base address:
<script type="text/javascript" src="scripts/jquery.js"></script>
It's often useful when you have multiple publishing destinations, like local dev web server, demo server, etc. You just replace that base URL.
<body>
<script language="javascript" src='<%= this.ResolveClientUrl("~/full/path/to/jquery.js") %>' type="text/javascript"></script>
</body>
Hello all I have a problem. I have a masterpage that all of my Content pages inherit from. Within this masterpage I have a script tag pointing to the Javascript file folder ~/Scripts/validation.js
On my content pages I use different usercontrols that require the use of many of the functions within the validation.js file however if I dont put the <script> tag and the Javascript functions within a contentholder on the contentpage the usercontrols do not see these functions and I get errors like OnNameValidation is not defined.
Of course I can copy the Javascript code into all of the pages but that's 30+ pages and a maintenance nightmare if I find a bug in one of the Javascript functions.
So the question (if you haven't already figured out from my long dissertation) is how can I declare the script tag with the path to the validation.js file so that contentpages and their usercontrols etc. can access the functions/code.
What you are trying to do should work, so I suspect that the path to your javascript file is wrong (without seeing your html code I can only assume). Keep in mind that you can only reference the javascript file like this: "~/Scripts/validation.js" if you have the link in a HEAD runat="server" tag. Without the runat="server" it won't find the file. You would have to do something like "../scripts/validation.js"
As a test I would try to call your javascript function in the masterpage, so you can rule out a bad file reference.
I picked up this tip from ScottGu here.
Add this to you user control which enables Intellisense in user controls but always evaluates false:
<% if (false) { %>
<script src="/Scripts/validation.js" type="text/javascript"></script>
<% } %>
I am currently doing this in my site by going to the Source code on the master page and putting the following in the head and outside of the ContentPlaceHolder.
<head>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
<script src="CP.js" type="text/javascript"></script>
</head>
The path you are assigning for your js file is probably not matching in all the pages.
script src="../JavaScript/Scriptaculous/scriptaculous.js"
It should have something like above this if you have separate folder for Scripts, MasterPages, Pages & Controls.
This may be a painfully simply question for which I will be mocked but I am having difficulty in using filepaths in master pages. I believe this is because if a page in a sub-directory to using the master page then the filepath is incorrect.
To fix this I need to get the filepath from the root but I can't seem to get it working.
I tried:
<script type="text/javascript" src="~/jQueryScripts/jquery.js"></script>
and
<script type="text/javascript" src="../jQueryScripts/jquery.js"></script>
No luck on either!
Any ideas on how I can tell it to get the filepath from the root?
I'm just assuming by filepath, you actually mean url (or uri, I forget which one is partial).
Without the ~, the first example should work. <script type="text/javascript" src="/jQueryScripts/jquery.js"></script> would cause the browser to request http://www.example.com/jQueryScripts/jquery.js (where www.example.com is your domain).
I believe you need to have runat=server in the <head> tag of the MasterPage for this URL rebasing to work.
<head runat="server">
First off the tilde in front is a asp.net thing for use in server controls and won't work in basic HTML.
Without getting into detailed explanations you could just use a slash (/) in front, and include the web app name if its not the root site.
Or you could put code in your master page for dynamically including scripts, and let it handle the pathing. Like:
public void AddJavascript(string javascriptUrl)
{
HtmlGenericControl script = new HtmlGenericControl("script");
script.Attributes.Add("type", "text/javascript");
javascriptUrl += "?v" + Assembly.GetExecutingAssembly().GetName().Version;
script.Attributes.Add("src", ResolveUrl(javascriptUrl));
Page.Header.Controls.Add(script);
}
The above code also appends the assembly version. I use this mostly for development so my javascript files get updated whenever I build.
You could use the Page.ResolveUrl method to get around this
for example:
<script type="text/javascript" src="<%=Page.ResolveUrl("~/jQueryScripts/jquery.js")%>"></script>