Calling ASP inside the JavaScript

Hello,

I am trying to make a HEAD request to IIS Server using the Java Script and MSXML. I cannot get the value in the HTTP Header. But I can able to use ASP to get the variable
sm_user. (when user login to the application, the valid userID is stored into sm_user variable.) using the following

<%=Request.ServerVariables("sm_user")%>

Now my question is, Can I use this ASP code inside the JavaScript. Once I receive the value from the asp, I can pass it to my javaScript.



function getUserId(){
     //-------------------------------------------------------------------------
     //
     //           Querying the HTTP-Header (IIS Server) using MSXML 2.0
     //
     //-------------------------------------------------------------------------
     //Creating the Object
     var myUser
     var xmlhttp
     /*@cc_on @*/

     /*@if (@_jscript_version >= 5)
            try {
             xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
            } catch (e) {
              try {
               xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
              } catch (E) {
               xmlhttp=false
              }
            }
       @else
            xmlhttp=false
       @end @*/
       if (!xmlhttp) {
           try {
            xmlhttp = new XMLHttpRequest();
           } catch (e) {
            xmlhttp=false
           }
       }

      xmlhttp.open("HEAD", "/index.html",true);
      xmlhttp.onreadystatechange=
           function() {
               if (xmlhttp.readyState==4) {
                    alert("The login userId is - "+
                         xmlhttp.getResponseHeader("sm_user"))
                  myUser = xmlhttp.getResponseHeader("sm_user")
               }
           }
      xmlhttp.send(null);
      return myUser;
 }

I would appreciate your help. Its very important for me. if you answer ASAP.

kalagappan
alagappanKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fritz_the_blankCommented:
If you want to pass a value captured in asp and use it client side, just store the value to a variable server side and then reassign to a client side variable like this:

<%

dim strVariable
strVariable = "Some text"

%>

<SCRIPT LANGUAGE=javascript>
<!--
var strSomeVariable = "<%=strVariable%>";

//-->
</SCRIPT>
0
fritz_the_blankCommented:
So in this case, if I undestand you, this might work:

<SCRIPT LANGUAGE=javascript>
<!--
var strSomeVariable = "<%=Request.ServerVariables("sm_user")%>";

//-->
</SCRIPT>
0
alagappanKAuthor Commented:
Let me try this and I will let you know.

<SCRIPT LANGUAGE=javascript>
<!--
var strSomeVariable = "<%=Request.ServerVariables("sm_user")%>";

//-->
</SCRIPT>
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

alagappanKAuthor Commented:
I just run the following javascript

function getUserID(){
   var test = "<%=5-2 %>";
  alert("ASP Test : " + test;
  return test;
}

I am getting the result as below

ASP Test : <%=5-2 %>

Would you please let me know the right syntax. I would appreciate it.

thank you,
alagappanK
0
fritz_the_blankCommented:
First, a small syntax error--

this:
alert("ASP Test : " + test;

should be:

alert("ASP Test : " + test);




I think that the problem there is that the result is not being interpreted as something valid. If you did this:

<%
intDifference = 5-2
%>

function getUserID(){
   var test = "<%=intDifference%>";
  alert("ASP Test : " + test);
  return test;
}

you might get what you expect.

Did this work? var strSomeVariable = "<%=Request.ServerVariables("sm_user")%>";


Fritz the Blank
0
alagappanKAuthor Commented:
As you said, I tried this

function getUserId(){
  var myID = "<%=Request.ServerVariables("sm_user")%>";
  alert("ASP Test : " + myID);
  return myID;
}

I am getting the   Error: Expected ';'

do you have any idea ??
0
fritz_the_blankCommented:
I will take a look...

Please refresh my memory: what is sm_user?

Fritz the Blank
0
fritz_the_blankCommented:
If you try this page, you'll see that it works fine:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
dim strName
strName = "Fritz the Blank"

%>
<SCRIPT LANGUAGE=javascript>
<!--
function sayHello(){
      alert("Hi " + "<%=strName%>");
}
//-->
</SCRIPT>

</HEAD>
<BODY onLoad=sayHello()>

<P>&nbsp;</P>

</BODY>
</HTML>
0
fritz_the_blankCommented:
You may have to do this:

function getUserId(){
  alert("ASP Test : " + "<%=Request.ServerVariables("sm_user")%>");
  return myID;
}
0
alagappanKAuthor Commented:
sm_user  is the userID Stored in the Server. I have a application running in the IIS Server. It is secured by the third party software SiteMinder by Netegrity.

Once user access the application, the Site Minder will provide the login page. User provide the userId and the password. This userId is stored in the sm_user.

I am interested in getting this userId and pass it to Javascript.

thank you. I am still working with your solution. I will let you know if it works. Let me know if anything I am making mistake.

thank you,
alagappanK
0
fritz_the_blankCommented:
I think that the last post I made should be it.

FtB
0
alagappanKAuthor Commented:
I tried the your last post. I think since, it have double quotes inside the double codes, its expection the ";" or ")".

the other thing is that I found this URL, would you please see this site. I am not clearly understand what they trying to say about calling serverside code from client side code.

http://www.codeproject.com/useritems/shared-side_javascript.asp?print=true

I truly appreciate your help,
thank you,
alagappanK
0
fritz_the_blankCommented:
If you try this, you'll see that it works:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
dim strName
strName = "Fritz the Blank"

%>
<SCRIPT LANGUAGE=javascript>
<!--
function sayHello(){
     alert("Hi " + "<%=strName%>");
}
//-->
</SCRIPT>

</HEAD>
<BODY onLoad=sayHello()>

<P>&nbsp;</P>

</BODY>
</HTML>


I would suggest ignoring that post for now as they are using server-side javascript instead of vbscript, and that is just going to confuse this thread....


FtB
0
alagappanKAuthor Commented:
Hello FtB,

I just wanted to let you know that, I am trying to use the ASP in the JavaScript (not VBScript). Let me try your last post. I will let you know
0
fritz_the_blankCommented:
Are you sure that you want to go down that road? It may confuse things and the result is essentially what we are trying to do here.

Just so I can understand the problem, what does this look like?

<%
Response.write(Request.ServerVariables("sm_user"))
%>
0
ofekeCommented:
I hope my answere is not irrelevent.
If I am not mistaken - you are trying to run this asp code client side.
An ASP generates the document that is sent to your browser and there for you can't use ASP variables within a javascript function called by user actions.

there for var test="<%=5-2%>" is taken literly. it is on the client side.

you may use (server side)

<script>
var SomeVar = "<%=(5-2)%>";

function getUserID
{
   alert(SomeVar);
}
</script>

If I understand your question this should be your answere.
0
fritz_the_blankCommented:
@ ofeke--

Try this and you will see that it works (you just have to keep clear in your mind what happens client side and what happens server side):

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
dim strName
strName = "Fritz the Blank"

%>
<SCRIPT LANGUAGE=javascript>
<!--
function sayHello(){
     alert("Hi " + "<%=strName%>");
}
//-->
</SCRIPT>

</HEAD>
<BODY onLoad=sayHello()>

<P>&nbsp;</P>

</BODY>
</HTML>


FtB
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alagappanKAuthor Commented:
Do I have to save the code you provided below as html or asp ??

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
dim strName
strName = "Fritz the Blank"

%>
<SCRIPT LANGUAGE=javascript>
<!--
function sayHello(){
     alert("Hi " + "<%=strName%>");
}
//-->
</SCRIPT>

</HEAD>
<BODY onLoad=sayHello()>

<P>&nbsp;</P>

</BODY>
</HTML>


I just tried with html and I am not getting anything back. let me know please
0
fritz_the_blankCommented:
as an .asp file

FtB
0
fritz_the_blankCommented:
otherwise, any code inside the <% %> delimiters won't work

FtB
0
fritz_the_blankCommented:
0
jaysolomonCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: fritz_the_blank {http:#9674348}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.