Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script position on page & calling a sub

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
US
I copied the following script from MSDN. One thing I noticed right away is that the sub only works if placed *after* the html - why?

The other thing is, the only way I could get the call to the sub to work was by placing the call within the <Script> tag of the sub; if I placed [ChangeElementText &quot;para2&quot;, &quot;I am the new text in the second paragraph.&quot;] in its own <Script> tag, nothing happened. How can I call the sub from outside its own tags?

<BODY>
<CENTER>
<H1>
This is the heading on a simple page.
</H1>
</CENTER>
<P ID=&quot;para1&quot;>
This is the first paragraph on a simple page.
</P>
<P ID=&quot;para2&quot;>
This is the second paragraph on a simple page.
</P>
</BODY>

<Script Language = vbscript>
ChangeElementText &quot;para2&quot;, &quot;I am the new text in the second paragraph.&quot;

Sub ChangeElementText(strID, strNewText)
Dim objElement

Set objElement = document.all(strID)
If Len(objElement.innerText) > 0 Then
objElement.innerText = strNewText
End If
End Sub
</Script>
 
The ASP is entered at the top. All code and HTML up until the the first procedure (SUB ,Function) is &quot;main line&quot; code. Once a procedure is encountered, then all code an HTML must be within a procedure.
Your main line script will not work before the HTML beacuse there is no document to modify yet.
Try
<HTML>
etc
</HTML>
<Script Language = vbscript>
'MAIN LINE Code
ChangeElementText &quot;para2&quot;, &quot;I am the new text in the second paragraph.&quot;
' OTHER MAIN LINE code
</Script>

<Script Language = vbscript>
ChangeElementText &quot;para2&quot;, &quot;I am the new text in the second paragraph.&quot;
Sub ChangeElementText(strID, strNewText)
Dim objElement

Set objElement = document.all(strID)
If Len(objElement.innerText) > 0 Then
objElement.innerText = strNewText
End If
End Sub
</Script>
Compare Code (Text)
Generate Sort in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top