Solved

Server-side Javascript appearing at top of page

Posted on 2010-08-12
4
267 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

735 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