PDA

View Full Version : Using JavaScript with ASP - document undefined???



stevejmu
Nov 16th, 2003, 08:32 PM
I am trying to open a file with some ASP scripting and send the content to my JavaScript... the file opens fine with ASP, but when I send the content to the JavaScript, the "document" is undefined. Both the ASP and the JavaScript are in a table in the body of my ASP page (HTML). Here is the code, please help if you can - why is "document undefined" (THAT'S THE ERROR MESSAGE I KEEP GETTING WHEN JAVASCRIPT IS CALLED FROM ASP)???


** THE ASP **
<%
Dim myContent
Set fso = Server.CreateObject("Scripting.FileSystemObject")
ConfigFile = Server.MapPath ("./tickerText.txt")
Set TheFile = fso.OpenTextFile (ConfigFile, 1)
myContent=TheFile.ReadLine
TheFile.Close
Set TheFile=Nothing
Set fso=Nothing
response.write startTicker(myContent) 'Calls the JavaScript(?)
%>

** THE JAVASCRIPT **
<script language="javascript" runat=server>
function startTicker(myContent){
var content=myContent;
var boxheight=150;
var boxwidth=150;
var boxcolor="#FFFFFF";
var speed=75;
var pixelstep=2;
var godown=false;
document.write(myContent);
}
</script>

Roelf
Nov 16th, 2003, 09:10 PM
you have runat=server in your script tag, the server has no document-object
My guess is the javascript should run clientside, delete the runat tag and it will work

stevejmu
Nov 16th, 2003, 09:17 PM
Thanks for your help - I took out the RUNAT=SERVER and now I'm getting some trouble with VBSCRIPT - here's the error... any idea how to invoke a javascript function inside VBSCRIPT???

** THE ERROR **

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'startTicker'

/tremper/index.asp, line 132

glenngv
Nov 17th, 2003, 05:48 AM
You can't call client-side js from server-side

** THE ASP **
<%
Dim myContent
Set fso = Server.CreateObject("Scripting.FileSystemObject")
ConfigFile = Server.MapPath ("./tickerText.txt")
Set TheFile = fso.OpenTextFile (ConfigFile, 1)
myContent=TheFile.ReadLine
TheFile.Close
Set TheFile=Nothing
Set fso=Nothing
%>

** THE JAVASCRIPT **
<script language="javascript">
function startTicker(){
var content="<%=myContent%>";
var boxheight=150;
var boxwidth=150;
var boxcolor="#FFFFFF";
var speed=75;
var pixelstep=2;
var godown=false;
document.write(content);
}
</script>

just make sure myContent doesn't have linefeeds and special chars like " \

stevejmu
Nov 17th, 2003, 02:16 PM
I've figured out how to get around this stupid issue. My biggest ordeal was trying to use a javascript scroller that a client could edit... the issue was that it's nearly impossible to code a scroller with VBScript AND it's impossible to read a file using JavaScript. So in order to combine my efforts, I use VBScript to edit a *.js file that contains the "content" variable for all the ticker lines of the scroller. Then I just embed the *.js source file with JavaScript - good times!

Thanks for all who helped :thumbsup: