Can I write ASP code in a .js file? - javascript

Can I write ASP code in a .js file. Or if there is any way through which I can update database in a .js file.

Technically, yes.
You need to add the js extension to IIS and the web.config file:
<system.web>
<httpHandlers>
<add verb="*" path="*.js" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>
The other option (which I recommend) is request an .asp file, like this:
<script type="text/javascript" src="yourASPfile.asp"></script>
And yourASPfile.asp would send a javascript header:
<%
Response.ContentType = "application/x-javascript"
%>
Although technically, you don't need to set the header.
Now you can write asp/javascript and it gets parsed by the server THEN delivered to the browser to be used.

If you mean that you want to use server-side scripting to generate your .js files, Dan's answer helps you there.
If you mean you want to use Javascript as your server-side ASP language, yes, you can do that. You can set it up by configuration (changing the default langauge from VBScript to JScript), or you can do it explicitly per-file. I do the latter, just because the overwhelming assumption is that .asp pages are written in VBScript. Here's an example:
<%#Language=JScript%>
<!DOCTYPE HTML>
<html>
<head>
<title>Example Javascript ASP Page</title>
</head>
<body>
<%
var hello;
hello = "Hello from Javascript!";
Response.Write("<p>" + hello + "</p>");
%>
</body>
</html>
I do this in any Classic ASP app I write, because that way I'm using the same language on the client and the server. I frequently code up a piece of abstract functionality that ends up being in both places (via includes). Very efficient. :-)

Related

Why is it printing all comments from included file to the screen?

I'm working with classic asp on server including the javascript file in the document like this:
<!--#include virtual="datoteke/jsPDF-master/jspdf.js"-->
...when i run my file on server, browser prints all the comments from included files to the screen. I tried to include it also like this:
<script type="text/javascript" src="jsPDF-master/jspdf.js"></script>
...it worked on localhost, but on server it doesn't, i get an error when i create an instance to the class in my javascript file: "[object Error]"
Why is it printing all those comments to the screen and how do I actually include javascript on server side? What did i do wrong?
The browser looks for JavaScript code in <script> tags. Your first statement does not seem to include those tags at all. I guess you want something like this:
<script type="text/javascript">
<!--#include virtual="datoteke/jsPDF-master/jspdf.js"-->
</script>
You should also know that the virtual directive makes a subrequest to load the file through the web server, which I suspect don't need/want. Give file a try.
Edit: There's a quite nice article about SSI in Wikipedia. Please note that IIS supports Server-Side Includes but it isn't related to ASP Classic at all.

javascript won't execute - only displays code

I'm trying to execute a javascript file in my browser but the code is displayed and not actually executed. I'm using firefox and I made sure javascript is enabled. I tried using a .js extension and .shtml and both just display the code. The file is located in my apache htdocs folder and it's version 2.2.
I'm trying to run this hello world code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" src="helloworld.js"></script>
</head>
<body>
<p id="hello"></p>
</body>
</html>
Here's the javascript
document.getElementById('hello').innerHTML = 'hello world';
Any suggestions?
You have no element with the ID of ex.
With the HTML you've shown, your line should be:
document.getElementById('hello').innerHTML = 'hello world';
See now that <p> tag has the id="hello" -- how would a call to getElementById('ex') be able to find that?
EDIT
Working JSFiddle example
try something like this
<html>
<head>
<script type="text/javascript" src="helloworld.js"></script>
</head>
<body onload="onload1()">
<p id="hello"></p>
</body>
</html>
helloworld.js
function onload1(){
document.getElementById('hello').innerHTML = 'hello world';
}
Save the above code as .htm or .html. Your server is probably not configured to use SSI (.shtml extension).
You said you saved it as .js and as .shtml, this is probably where you have gone wrong. .js files will not display properly when opened directly by the browser, as the browser is not intended for displaying JavaScipt, thus you must use a file type that is intended for HTML. The .shtml extension is for HTML, however it is only used with servers using SSI (Server Side Includes), so if the server does not use SSI it will not work.
Usually you will see code when the mime type is wrong. The mime type is how the server tells the browser what kind of file it is sending http://en.wikipedia.org/wiki/Internet_media_type. The server sends the mime type, which it determines by looking a the files extension, or in some cases certain parts of the file (and server side code can adjust the mime type as well). The server will give files with the .htm and .html extension the HTML mime type, but .js will not have that type. The .shtml file will only be given the HTML mime type if the server uses SSI. So when the browser gets those files it will intemperate them as plain text. This is why you see the code.
So either change the extension to .htm or .html, or enable SSI on the server (or ensure it is configured to work in that directory).
As a side note, your JS will not do anything as there is no element with the ID 'ex', however that should not produce the problem of seeing code. It will however give you trouble down the line.

Using ASP.Net tags in a .js file?

Is it possible to embed <% ... %> tags in a javascript file and have it render the appropriate server-side code? How can this be done?
The process is actually backwards from what you're thinking.
You create an *.aspx page that renders to a JavaScript file. You can then reference that *.aspx page in your <script> tag:
<script type="text/javascript" src="someJavaScript.aspx"></script>
Yes, this is possible, but it would not be a JS file, it would be an aspx file with Javascript in it instead of html.
In your page, to refrence it you would do:
<script type="text/javascript" src="myPage.aspx"></script>
I'm going to make some assumptions on what you are trying to accomplish. Most likely you have a javascript file that needs access to some info on the server. Let's say you need some stuff from the session that if it were an aspx page you'd make it look something like
<script type="text/javascript">
var username = '<%= Session["username"] %>';
var account_num = '<%= Session["account_num"] %>';
</script>
obviously this won't work in a .js file since it never goes through the page lifecycle that an aspx page would be processed though. However, you don't need to transform your entire .js file into an .aspx page as some others might be suggesting. There are lots of other ways to expose that data to your js code. I'll list 2.
emit the above <script> in your page response (perhaps using a <asp:ContentPlaceHolder />)
create a webservice (could even be a simple .ashx) that returns the var username = ... or even better returns json
Yes, it's possible by simply making a regular web page that contains Javascript instead.
However, it might not behave like you expect. Javascript files are cached longer than pages. As the browser might not request the file from the server each time, your could would not be executed each time the file is used.

How to add a script link specific to a page (MVC using master pages)

I'm a little confused with inclusion of Master Pages and Script tags. I have several script (*.js) files that I want to include, and not all the files are relevant for each page. I'm using Master page, and it seems that I can only do this in the master page.
But I see this as a waste, and I'm thinking I need to include all the JS files in the master page. Which means that I will be loading up the JS files without using them in all the pages.
Is there a better way to do this ?
First add a ContentPlaceHolder in your master page :
<head runat="server">
<title>My awesome master page</title>
<script type="text/javascript" src="global.js"></script>
<asp:ContentPlaceHolder ID="foo" runat="server" />
</head>
Add additional scripts from your page through the ContentPlaceHolder
<asp:Content ContentPlaceHolderID="foo" runat="server">
<script src="specific.js" type="text/javascript"></script>
</asp:Content>
You can add some properties to ViewModel to flag what js files you need.
In production I'd suggest just to merge all of your js files into one and use js minifier.
This will reduce number of calls to server for static resources.
If your js files size is not big, I'd suggest using second option.
There is another option, but it is more complicated. You can create http extension, which will in fluent way load your js files and combine them according to page needs and push them directly into response.
Another way that might be more suitable to the one suggested by tkalve, to do this programatically...
HtmlLink jsLink = new HtmlLink();
jsLink.Href = "~/scripttoload.js";
jsLink.Attributes.Add("type", "text/javascript");
Header.Controls.Add(jsLink);
however header of the page (master page) must be declared as runat="server" to be able to do this. Check out http://odetocode.com/code/450.aspx which provides a very usefull discussion of all kinds of master-pages issues, including several ways of solving this one.

ServerSide JavaScript

I have a question about SSJS.
With SSJS, is it possible to hide code from the user?
Other ServerSide languages like PHP aren't viewable in the source because they are processed before the client side, the browser
A little example of what I want:
<html>
<head>
<script runat="server">
function getPassword(){
var password = "myPass";
return password;
}
</script>
</head>
<body>
<script>
alert(getPassword());
</script>
</body>
</html>
I tested this, but the password is still viewable
Am I doing something wrong so that my example is simple CSJS or is it impossible to hide SSJS-code?
Adhering to the wise words of T.J. Crowder, your file could be a classic .asp file on a windows based host (so a file with the extension .asp), looking like this:
<% #language=ecmascript %>
<%
function getPassword(){
var password = "myPass";
return password;
}
%>
<html>
<head>
</head>
<body>
<script>
alert('<%=getPassword()%>');
</script>
</body>
</html>
Or more in line with your style it could look like:
<script language="jscript" runat="server">
// mind the 'language' property, it is required.
// The script tag doesn't have to be
// in the header of the html-document.
function getPassword(){
var password = "myPass";
return password;
}
</script>
<html>
<head>
</head>
<body>
<script>
alert('<%=getPassword()%>');
</script>
</body>
</html>
Both ways the server side scripting will not be visible in the page source. Aside: <%=...%> can be viewed as shorthand for Response.Write(...)
For other hosts, check your provider or the wikipedia list given in David Dorwards answer
Yes, your server-side code can be hidden from the user, exactly as with any other server-side language. You have to serve the relevant HTML file via a server that understands server-side JavaScript, and you have to configure that server correctly (by default, .html files probably aren't going to be pre-processed; typically HTML files with server-side code would have a different extension, such as .shtml, .asp, .aspx, etc., depending on the platform on which you're running them; although of course with proper configuration you can have anything handled correctly). If you were able to see the code above via a web browser, then you've missed out one of those steps.
Note that server-side JavaScript will have the benefits, and disadvantages, of any other server-side language. You can't (for instance) do any scripting of the client's browser with any server-side language, which is why you see so much client-side code around. (Perhaps that was obvious. :-) )
Any server side JavaScript will only be available on the server but, just as you need to run PHP through a PHP engine before sending it to the client, you have to run SSJS through a JS engine before sending it on.
You can find a list of such engines at Wikipedia, although node.js is probably the most popular at present.
runat="server" is, IIRC, an ASP.NET construct. If you want to go down that route, you'll need to be using ASP.NET for a start. I've no idea if the syntax you have is right though.

Categories

Resources