Solved

Server-side Javascript appearing at top of page

Posted on 2010-08-12
4
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

630 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