Solved

Calling ASP inside the JavaScript

Posted on 2003-11-03
23
504 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:alagappanK
23 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9673461
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9673470
So in this case, if I undestand you, this might work:

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

//-->
</SCRIPT>
0
 

Author Comment

by:alagappanK
ID: 9673535
Let me try this and I will let you know.

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

//-->
</SCRIPT>
0
 

Author Comment

by:alagappanK
ID: 9673587
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9673624
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
 

Author Comment

by:alagappanK
ID: 9673755
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9673782
I will take a look...

Please refresh my memory: what is sm_user?

Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9673806
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9673816
You may have to do this:

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

Author Comment

by:alagappanK
ID: 9673861
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9673874
I think that the last post I made should be it.

FtB
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:alagappanK
ID: 9674037
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9674116
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
 

Author Comment

by:alagappanK
ID: 9674278
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9674298
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
 

Expert Comment

by:ofeke
ID: 9674328
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
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 500 total points
ID: 9674348
@ 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
 

Author Comment

by:alagappanK
ID: 9674419
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9674441
as an .asp file

FtB
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9674449
otherwise, any code inside the <% %> delimiters won't work

FtB
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9751441
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10346729
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

760 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

25 Experts available now in Live!

Get 1:1 Help Now