Solved

Access an ASP value from Javascript

Posted on 2007-04-10
21
184 Views
Last Modified: 2012-05-05
Could someone help me with this little issue Im having.  Basically I am attempting to reference a html form value from a javascript file.  The problem is that the value for the form field comes from an ASP value, which I dont think the javascript can access.

Heres the HTML
<input type="text" name="OutputFile" value="<%=objMapSvr.OutputFile%>">
And heres the contents of the Javascript file embed.js

var sACGM = document.forms.MapValues.OutputFile.value;

document.writeln ("<object id='AXCGM' classid='clsid:F5D98C43-DB16-11CF-8ECA-0000C0FD59C7' width='100%' height='100%' viewastext> <param NAME='FileName' VALUE=sACGM> <embed name='AXCGM' src=sACGM width='100%' height='100%'> </embed> </object>")
0
Comment
Question by:BillPowell
  • 11
  • 10
21 Comments
 
LVL 11

Expert Comment

by:flipz
ID: 18883552
What you need to do can be accomplished - just add an ID value in your textbox control the same as the name and change the first line in you JS - I am assuming your form has and ID value of MapValues since you didn't post that part.

<input type="text" name="OutputFile" id="OutputFile" value="<%=objMapSvr.OutputFile%>">
And heres the contents of the Javascript file embed.js

var sACGM = document["MapValues"].OutputFile.value;

document.writeln ("<object id='AXCGM' classid='clsid:F5D98C43-DB16-11CF-8ECA-0000C0FD59C7' width='100%' height='100%' viewastext> <param NAME='FileName' VALUE=sACGM> <embed name='AXCGM' src=sACGM width='100%' height='100%'> </embed> </object>")
0
 
LVL 11

Author Comment

by:BillPowell
ID: 18883578
Here is the form itself:
<form NAME="MapValues" action="GenerateMap.asp" target="frmHid" method=post>
0
 
LVL 11

Expert Comment

by:flipz
ID: 18883618
Just add ID value - let me know if you have problems when you make the changes:

<form NAME="MapValues" ID="MapValues" action="GenerateMap.asp" target="frmHid" method=post>
0
 
LVL 11

Expert Comment

by:flipz
ID: 18883627
BTW - you may have to encode the output value from ASP so that you don't get any problem characters:

<input type="text" name="OutputFile" id="OutputFile" value="<%=Server.HTMLEncode(objMapSvr.OutputFile)%>">
0
 
LVL 11

Author Comment

by:BillPowell
ID: 18883776
Something I should mention to you while Im troubleshooting that might make a difference.  This website uses frames.  The page with the form I mentioned is only one of the frames.  The other thing I should mention is that using the javascript file to embed an object is my workaround for avoiding the extra click required to activate an Active X control.
0
 
LVL 11

Expert Comment

by:flipz
ID: 18883890
I don't think that changes anything but I'm certainly interested in seeing if your theory works - I could make use of the same technique. As long as your embed.js file is in the same frame as the form then all should be fine.
0
 
LVL 11

Author Comment

by:BillPowell
ID: 18884790
The embed.js is in the same asp page.  The problem Im having now is that upon generation of the page and while attempting to embed the control, it seems to hang.  Theres no error on the status bar, it just does not appear as it should.

By the way, this is not my theory.  Its actually a well known technique for bypassing that bug.  I got the idea from another colleage, who has it working in several other sites.  So this should work, except that I probably have something wrong with the way Ive implemented it.
0
 
LVL 11

Expert Comment

by:flipz
ID: 18884985
Looks like the value for the param tag is not being concatenated properly - try this:

document.writeln ("<object id='AXCGM' classid='clsid:F5D98C43-DB16-11CF-8ECA-0000C0FD59C7' width='100%' height='100%' viewastext> <param NAME='FileName' VALUE='" + sACGM + "'> <embed name='AXCGM' src='" + sACGM + "' width='100%' height='100%'> </embed> </object>")
0
 
LVL 11

Author Comment

by:BillPowell
ID: 18885231
Thats certainly one issue.  Heres the total contents of my embed.js
I have added an alert to see if we were successful in getting the value from the form.  When I run the code, it doesnt get to the alert, so something goes wrong before then.

var sACGM = document["MapValues"].OutputFile.value;
alert(sACGM);
document.writeln ("<object id='AXCGM' classid='clsid:F5D98C43-DB16-11CF-8ECA-0000C0FD59C7' width='100%' height='100%' viewastext> <param NAME='FileName' VALUE='" + sACGM + "'> <embed name='AXCGM' src='" + sACGM + "' width='100%' height='100%'> </embed> </object>")
0
 
LVL 11

Expert Comment

by:flipz
ID: 18889099
Can you post the outputted value for objMapSvr.OutputFile ?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 11

Author Comment

by:BillPowell
ID: 18889491
Heres a typical example(with url modified).  The actual cgm filename will always be different.

        <object id="AXCGM" classid="clsid:F5D98C43-DB16-11CF-8ECA-0000C0FD59C7" width="1020" height="790" viewastext>
          <param NAME="FileName" VALUE="http://mapping.mysite.ca//MapCache/{A04CCAC7-1303-45A5-85A8-1C55A9BE920E}.cgm">
          <embed name="AXCGM" src="http://mapping.mysite.ca//MapCache/{A04CCAC7-1303-45A5-85A8-1C55A9BE920E}.cgm" width="1020" height="790"></embed>
        </object>
0
 
LVL 11

Expert Comment

by:flipz
ID: 18889537
This appears to work for me - try:

<form NAME="MapValues" ID="MapValues" action="GenerateMap.asp" target="frmHid" method=post>
<input type="text" name="OutputFile" id="OutputFile" value="http://mapping.mysite.ca//MapCache/{A04CCAC7-1303-45A5-85A8-1C55A9BE920E}.cgm">
</form>

<script language="JavaScript">
var sACGM = document["MapValues"].OutputFile.value;
alert(sACGM);
document.writeln ("<object id='AXCGM' classid='clsid:F5D98C43-DB16-11CF-8ECA-0000C0FD59C7' width='100%' height='100%' viewastext> <param NAME='FileName' VALUE='" + sACGM + "'> <embed name='AXCGM' src='" + sACGM + "' width='100%' height='100%'> </embed> </object>")
</script>
0
 
LVL 11

Author Comment

by:BillPowell
ID: 18891504
Thanks for your help so far, I think Im quite close to getting it to work.  Could you tell me how I should change the first line of the embed.js file, if I needed to get the value this way instead:

'instead of this
var sACGM = document["MapValues"].OutputFile.value;

'I would need to retrieved from an asp request object
var sACGM = <%=Request.Form("OutputFile")%>
0
 
LVL 11

Expert Comment

by:flipz
ID: 18891607
I am not sure if you can do that or not. Since the file you are trying to retrieve this value in is a .js file - I'm not sure if the ASP engine will try to process it - it might but I'm not sure. You can try your suggestion but you'll need to put quotes around it:

var sACGM = "<%=Request.Form("OutputFile")%>";

If possible you can always put the javascript in a script tag in your ASP page instead but then you lose some of the 'reuseability' of the code. Let me know how it goes.
0
 
LVL 11

Author Comment

by:BillPowell
ID: 18891632
No problem, I will try to get this to work putting the javascript tag right in the page just to see if it works.  Is this the correct syntax to do that.

var sACGM = "<%=Request.Form("OutputFile")%>";
0
 
LVL 11

Expert Comment

by:flipz
ID: 18891702
Looks good to me - as long as there are no double quotes in the Request.Form("OutputFile") value.
0
 
LVL 11

Author Comment

by:BillPowell
ID: 18891850
Were very close now.  The embed.js file is working and getting right variable.  I placed an alert(sACGM); before the last line and confirmed the value in the message box is correct.  So there can only be something syntactically wrong with this last line of the script file:

document.writeln ("<object id='AXCGM' classid='clsid:F5D98C43-DB16-11CF-8ECA-0000C0FD59C7' width='100%' height='100%' viewastext> <param NAME='FileName' VALUE='" + sACGM + "'> <embed name='AXCGM' src='" + sACGM + "' width='100%' height='100%'> </embed> </object>")
0
 
LVL 11

Expert Comment

by:flipz
ID: 18891905
Can you verify that the src file path is valid? If you actually hardcode the object tag with the proper src - does it render properly?
0
 
LVL 11

Author Comment

by:BillPowell
ID: 18892229
Yes, the source is valid.
0
 
LVL 11

Accepted Solution

by:
flipz earned 500 total points
ID: 18892303
Put the output to a message box and see if it looks right:

alert ("<object id='AXCGM' classid='clsid:F5D98C43-DB16-11CF-8ECA-0000C0FD59C7' width='100%' height='100%' viewastext> <param NAME='FileName' VALUE='" + sACGM + "'> <embed name='AXCGM' src='" + sACGM + "' width='100%' height='100%'> </embed> </object>");

Just noticed something as well - there was no semi-colon on the end of the last line - I think JS is picky in that regards - either you always use them or you don't - no in between!! Try adding the semi-colon to see if it makes any difference.
0
 
LVL 11

Author Comment

by:BillPowell
ID: 18915173
After several more troubleshooting steps I finally got it to work.
Thanks for your help :)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now