Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Server-side Javascript appearing at top of page

Posted on 2010-08-12
4
Medium Priority
?
276 Views
Last Modified: 2013-11-19
I'm trying to get my Javascript to execute on the server instead of on the client-side. I've gotten it to work but for some reason my Javascript appears at the top of the page, above everything else instead of where it is placed in the page. If you look below there are two codes. The first is the code before execution and the second is the source code as seen by the browser. You'll notice that the Javascript message in the body of the code appears at the very top of the client side browser code. Can someone explain to me why this is happening and how I can get the Javascript to appear where I want it to on the page? Thanks



This is the code before going to server or browser:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
Server generated:
<script language="javascript" runat="server">
Response.Write("This is a server-side message");
</script>
</body>
</html>


This is the code as it appears in the browser source code: The message "This is a server-side message" is right at the top of the page.

This is a server-side message
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
Server generated:
</body>
</html>
0
Comment
Question by:elliottbenzle
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
sam2912 earned 1600 total points
ID: 33426841
This is because Response.Write is not JavaScript code. Server executes that code first (which writes your string to the response buffer), afterward, the html code is then placed into the response buffer.

Why do you need JavaScript to run at server-side? JavaScript is meant for client-side usage.
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 400 total points
ID: 33427272
I am curious too.

Also why have the page language as VBScript and then put a server side JS on the page too?


0
 
LVL 4

Author Comment

by:elliottbenzle
ID: 33429655
I'm trying to run Javascript on the serve side as a work-around to a problem which involves connecting to a database using javascript, but since the connection and subsequent display of data is done after the page is loaded none of the data is crawlable by search engines. My idea is to run the connection and data grab before the page is loaded. Although it looks like this may not be the solution I'm looking for.

BTW, I changed the language at the top to Javascript and the code was placed in the correct place on the page.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33429690
PS: Sam
response.write("Text)
is the server side javascript equivalent of vbscript's
response.write "Text"
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question