Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi,
I want to read a property from the code behind in my javascript on load, and i am doing it happily like this:

<script language="javascript">
    $(window).load(function() {
        if ('<%= IsNewRecord %>' == 'True') {
            alert("new record");
        }
    });
</script>


where property 'IsNewRecord' is in my code behind like below:

protected bool IsNewRecord
   {
       get { return true; }
   }


It works fine till here, but when i move my javascript code to a external file say test.js and reference it in my script tag on the page, the behaviour stop working with no errors.

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


Why ?
Posted
Updated 26-May-11 16:23pm
v2
Comments
Wonde Tadesse 26-May-11 22:44pm    
How do you know it's referenced correctly ? Have you tried to call another function or ?
saxenaabhi6 26-May-11 22:56pm    
i did put an alert on the top of my external test.js its
alerting that.
Wonde Tadesse 26-May-11 23:22pm    
Good to know that at least something is working :). See Yvan Rodrigues or --SA answer.
saxenaabhi6 26-May-11 23:35pm    
Thats called bog down, i should have stuck to the 'on page js' and moved forward :P
Yvan Rodrigues 27-May-11 8:10am    
We've all done that many, many, many times. It's a good thing (in small doses). It's lazy to just accept that something works if it could be better. A Ford Escort works. A Porsche Carrera is fast and beautiful.

When a web browser wants a web page it issues an HTTP GET to the server for the html file, or in this case, your aspx file. The web server, in this case IIS, knows that the file that the client requested is an ASP.NET web form by identifying the .aspx file extension. It uses that as a cue that it needs to execute the dynamic content in your web form, and send the output to the TCP stream.

When the client browser receives your HTML, it parses it, and makes a list of all external resources in the HTML such as CSS, images, and your javascript. It then issues a separate HTTP GET for each of those resources. Once it has received them all it assembles and renders the page to the user.

In that previous step the browser made a request for your .js file. The server received the request and happily delivered the javascript to your browser. Since the file extension is .js, not .aspx it didn't know that you wanted it to parse the javascript and look for your codebehind reference. It just delivered the goods.

You have three options.

1. Just embed the javascript in the html file like you did originally.

2. Rename the standalone javascript file to a .aspx extension and in the .aspx add a ContentType @Page directive[^] to text/javascript. In your HTML, link to the .aspx file.
<script src="scripts/test.aspx" type="text/javascript"></script>

IIS will now parse and compile your javascript document, correctly rendering your codebehind; since the MIME content type is correctly set, the browser will treat it as javascript, even though it has a .aspx extension.

3. In the IIS configuration (or web.config) you can configure IIS so that it treats all .js files as web forms.

How nice, THREE options!
 
Share this answer
 
v3
Comments
saxenaabhi6 26-May-11 23:02pm    
let me test your 2nd option, i never heard of that.
Yvan Rodrigues 26-May-11 23:09pm    
It works for any type of file. For example I have an aspx file that takes text as input and outputs the text in a fancy font for headlines. I just use <img src="myfancyheadline.aspx" /> and set the content type to image/png and it works 100% of the time.

Want to generate CSS dynamically, do it!
Want a dynamically created SVG diagram? No problem!

Option 3 looks tidier, but you need to have sufficient rights on the server.
saxenaabhi6 26-May-11 23:25pm    
i tried changing my .js file to .aspx file like below
<code lang="javascript">
<%@ Page ContentType="text/javascript" %>
alert("coming here");
$(window).load(function() {
if ('<%= IsNewRecord %>' == 'True') {
alert("new record");
}
});
</code>

and referenced this aspx in script tag... it dosent work...
when build it says 'IsNewRecord' is not declared on your renamed aspx page.

Rather then renaming i also tried thi approach by creating a new jtest.aspx page and following all the steps, but no luck
Wonde Tadesse 26-May-11 23:18pm    
5+
Sergey Alexandrovich Kryukov 26-May-11 23:36pm    
All correct, a 5.
--SA
Isn't it obvious? Look at the part '<%= IsNewRecord %>'. This part of code is not Javascript. It is generated by your code behind, so your page is generated with either

JavaScript
if ('True' == 'True') {
    alert("new record");
}


or

JavaScript
if ('False' == 'True') {
    alert("new record");
}


So, Javascript code is generated dynamically depending on that IsNewRecord returns and results in valid Javascript code, one or another, different on each HTTP request.

When you put this file outside of you *.ASPX file, the code is delivered on the request for the same page as is, which is not a valid Javascript code. Javascript interpreter fails, your server hides error processing (which is the normal option for production), so you simply don't get your processing.

—SA
 
Share this answer
 
v3
Comments
Yvan Rodrigues 26-May-11 22:51pm    
I concur :)
saxenaabhi6 26-May-11 23:00pm    
hmmm.... I got what you said but i was thinking that aspx page will load properties first and then might load javascript file resolving the property...
any ways to set loading preferences.
Sergey Alexandrovich Kryukov 26-May-11 23:35pm    
What you're talking about has nothing to do with ASP.NET. The page is generated on HTTP request, client's browser reads HTML file which is pure HTML and sees the resource to be loaded in an external file. This is another HTTP request which delivers the file.
--SA
saxenaabhi6 26-May-11 23:38pm    
yaa go it.... the js will come just like a banner image, no processing once rendered.. Thanks :) my 5
Sergey Alexandrovich Kryukov 26-May-11 23:55pm    
You're welcome.
If you got it, will you please formally accept the answer (green button)?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900