Solved

Server-side Javascript appearing at top of page

Posted on 2010-08-12
4
265 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 400 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 100 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

776 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