Solved

Server-side Javascript appearing at top of page

Posted on 2010-08-12
4
263 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
PS: Sam
response.write("Text)
is the server side javascript equivalent of vbscript's
response.write "Text"
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now