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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Document.write throwing invalid character error

Status
Not open for further replies.

Vachaun22

Programmer
Oct 7, 2003
171
US
I am having a problem using the document.write function to write out some HTML code to a hta file. When I run it, it throws the invalid character error.

I'm assuming the invalid character is caused by quotation marks that are in the string variable. Is there anyway to successfully retain those quotations in the string? Here is part of the string that needs outputted:

Code:
ul.tree{
	display:none;
	margin-left:17px;
}

li.folder{
	list-style-image: url("images/plus.gif");
}

li.folderOpen{
	list-style-image: url("images/minus.gif");
}

li.file{
	list-style-image: url("images/dot.gif");
}

a.treeview{
	color:Navy;font-family:verdana;font-size:8pt;
}

a.treeview:link {
	text-decoration:none;
}

a.treeview:visited{
	text-decoration:none;
}

a.treeview:hover {
	text-decoration:underline;
}
</style>

<ul id="N">
	<li class="folder" id="P1">

I got this from a textarea that I set the .value property equal to the string variable I'm trying to use with document.write function.
 
Which code raises the error ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You can try to...

Add two "

""images/dot.gif""

or change to a '

'images/dot.gif'

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
The actual code that causes the error is:

Code:
Document.Write s

The value of s is as copied from the textarea:

Code:
<div>
<script>



function toggle(id,p)
{
	var myChild = document.getElementById(id);
	
	if(myChild.style.display!='block')
	{
		myChild.style.display='block';
		document.getElementById(p).className='folderOpen';
	}
	else
	{
		myChild.style.display='none';
		document.getElementById(p).className='folder';
	}
}
</script>

<style>

ul.tree{
	display:none;
	margin-left:17px;
}

li.folder{
	list-style-image: url("images/plus.gif");
}

li.folderOpen{
	list-style-image: url("images/minus.gif");
}

li.file{
	list-style-image: url("images/dot.gif");
}

a.treeview{
	color:Navy;font-family:verdana;font-size:8pt;
}

a.treeview:link {
	text-decoration:none;
}

a.treeview:visited{
	text-decoration:none;
}

a.treeview:hover {
	text-decoration:underline;
}
</style>

<ul id="N">
	<li class="folder" id="P1">
		<a class="treeview" href="javascript:void(0);" onclick="toggle('N1','P1');" target="content" title="Accounting Organizational Unit">Accounting</a>

		<ul id="N1" class="tree">
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
		</ul>
	</li>

	<li class="folder" id="P4">
		<a class="treeview" href="javascript:void(0);" onclick="toggle('N4','P4');" target="content" title="Administrative Organizational Unit">Administrative</a>

		<ul id="N4" class="tree">
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
	
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
		</ul>
	</li>

	<li class="folder" id="P8">
		<a class="treeview" href="javascript:void(0);" onclick="toggle('N8','P8');" target="content" title="Billing Organizational Unit">Billing</a>

		<ul id="N8" class="tree">
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
		</ul>
	</li>

	<li class="folder" id="P12">
		<a class="treeview" href="javascript:void(0);" onclick="toggle('N12','P12');" target="content" title="Dispatch Organizational Unit">Dispatch</a>

		<ul id="N12" class="tree">
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
		</ul>
	</li>

	<li class="folder" id="P21">
		<a class="treeview" href="javascript:void(0);" onclick="toggle('N21','P21');" target="content" title="IT Organizational Unit">IT</a>

		<ul id="N21" class="tree">
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
		</ul>
	</li>

	<li class="folder" id="P24">
		<a class="treeview" href="javascript:void(0);" onclick="toggle('N24','P24');" target="content" title="Payroll Organizational Unit">Payroll</a>

		<ul id="N24" class="tree">
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
		</ul>
	</li>

	<li class="folder" id="P26">
		<a class="treeview" href="javascript:void(0);" onclick="toggle('N26','P26');" target="content" title="Reception Organizational Unit">Reception</a>

		<ul id="N26" class="tree">
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
		</ul>
	</li>

	<li class="folder" id="P28">
		<a class="treeview" href="javascript:void(0);" onclick="toggle('N28','P28');" target="content" title="Safety Organizational Unit">Safety</a>

		<ul id="N28" class="tree">
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>

			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
		</ul>
	</li>

	<li class="folder" id="P32">
		<a class="treeview" href="javascript:void(0);" onclick="toggle('N32','P32');" target="content" title="Sales Organizational Unit">Sales</a>

		<ul id="N32" class="tree">
			<li class="file">
				<a class="treeview" href="" target="content">User Name</a>
			</li>
		</ul>
	</li>
</ul>
</div>
 
I'm not really sure if that's why you are getting the error. I'm able to put quotes in a textarea then write it out using document.write. Can you post the code you are using to write out the variable? Thanks.

Ron Wheeler
 
Sure, the code I am using is taken from another project that was created for ASP, so I just did the little bit necessary to create a .vbs file instead of .asp, but it is someone else's work...

Code:
Option Explicit
' ======================================================================================
' Desc:		This code show how to create a simple treeview class using ASP and Cascading Stylesheets.
'			Great for programmers who want to learn how to create simple ASP controls.
'
' Author:	Tanwani Anyangwe (tanwani@aspwebsolution.com)
'
' Requires: ASP 2.1 +
'
' Copyright © 2001 Tanwani Anyangwe for AspWebSolution.com
' --------------------------------------------------------------------------------------
' Visit AspWebSolution - free source code for ASP & AS.NET programmers
' [URL unfurl="true"]http://aspwebsolution.com[/URL]
' --------------------------------------------------------------------------------------
'
' Please ensure you visit the site and read their free source licensing
' information and requirements before using their code in your own
' application.
'
' ======================================================================================

'To keep a global count of howmany nodes have been created
Dim NODE_COUNTER : NODE_COUNTER = 1

'String buffer - way faster than concatenating strings (e.g. string1 & string2 - use buffer.Append(myString))
Class Buffer
    Private dic
    Private counter
    
    Private Sub Class_Initialize()
        counter = 0
        'Set dic = Server.CreateObject("Scripting.Dictionary")
        Set dic = CreateObject("Scripting.Dictionary")
    End Sub
    
    Private Sub Class_Terminate()
        Set dic = Nothing
    End Sub
    
    Private Function GetKey()
        GetKey = CStr(counter + 1)
        counter = counter + 1
    End Function
    
    Public Sub Append(vStr)
        Call dic.Add(GetKey(), vStr)
    End Sub
    
    Public Function ToString()
        ToString = Join(dic.Items)
    End Function
End Class


Class Collection
	Private m_next,m_len
	Private m_dic	
	
	Public Sub Add(Item)
		m_dic.Add "K" & m_next,Item
		m_next = m_next+1		
		m_len = m_len+1		
	End Sub
	
	Public Sub Clear
		m_dic.RemoveAll 
	End Sub
	
	Public Function Length
		Length=m_len
	End Function
	
	Public Default Function Item(Index)
		Dim tempItem,i
		For Each tempItem In m_dic.Items 
			If i=Index Then
				Set Item=tempItem
				Exit Function
			End If
			i=i+1
		Next	
	End Function
	
	Public Sub Remove(ByVal Index)
		Dim Item,i
		For Each Item In m_dic.Items 
			If i=Index Then
				m_dic.Remove(Item)
				m_len=m_len-1
				Exit Sub
			End If
			i=i+1
		Next			
	End Sub
	
	Private Sub Class_Initialize
		m_len=0
		'Set m_dic = Server.CreateObject("Scripting.Dictionary")
		Set m_dic = CreateObject("Scripting.Dictionary")
	End Sub
	
	Private Sub Class_Terminate
		Set m_dic = Nothing				
	End Sub	
	
	Public Function GetXml(parentid)
		Dim buf
		Set buf = New Buffer
		Dim item
		'buf.Append("<items>")
		For Each item In m_dic.Items 			
			buf.Append(item.GetXml(parentid))			
		Next	
		'buf.Append("</items>") 
		GetXml = buf.ToString
		Set buf = Nothing
	End Function
End Class

Class Node	
	'Public Parent
	Public Text
	Public Href
	Public Target
	Public ToolTipText
	Public ChildNodes
	Public ImageUrl
	Public ID
	
	Public Sub Init(strText,strHref,strToolTipText)
		Text=strText
		Href=strHref
		ToolTipText=strToolTipText
	End Sub
	Public Sub Add(objNode)
		ChildNodes.Add(objNode)
	End Sub
	
	Private Sub Class_Initialize
		Set ChildNodes = New Collection		
		ID = NODE_COUNTER		
		NODE_COUNTER = NODE_COUNTER + 1
	End Sub
	
	Private Sub Class_Terminate
		Set ChildNodes = Nothing				
	End Sub
	
	Public Function GetXml(parentId)
		Dim buf
		Set buf = New Buffer
		
		buf.Append "<item><menuid>" & Me.ID & "</menuid><text><![CDATA[" & Me.Text & "]]></text>"
		buf.Append "<tooltip>" & Me.ToolTipText & "</tooltip><target>" & Me.Target & "</target>"
		buf.Append "<url>" & Me.Href & "</url><imageurl>" & Me.ImageUrl & "</imageurl>"
		buf.Append "<parentid>" & parentId & "</parentid>"
		buf.Append("</item>") 
		buf.Append(ChildNodes.GetXml(Me.ID))
		
		GetXml = buf.ToString
		Set buf = Nothing
	End Function
End Class

Class TreeView
	
	Private m_folder
	Public Color	
	Public Nodes
	Public DefaultTarget
	Public ID
	Public MAX_RECORDS
	
	Private m_strXml
	Private m_strXsl
	
	Public Property Let ImagesFolder(strFolder)
		m_folder=strFolder
	End Property
	Public Property Get ImagesFolder()
		ImagesFolder=m_folder	
	End Property
	
	Private Sub Class_Initialize
		Set Nodes = New Collection	
		Color="Navy"
		m_folder="images"	
		MAX_RECORDS = 2000		
		m_strXsl = "treeview.xsl"	
	End Sub
	
	Private Sub Class_Terminate
		Set Nodes = Nothing				
	End Sub
	
	Public Function AddNode(Text)
		Dim tn 
		Set tn = new Node
		tn.Text=Text
		Nodes.Add(tn)
	End Function
	
	Public Function CreateNode(Text,Href,ToolTipText)
		Dim tn 
		Set tn = new Node
		Call tn.Init(Text,Href,ToolTipText)
		Set CreateNode=tn
	End Function
	Public Function CreateSimpleNode(Text)
		Dim tn 
		Set tn = new Node
		tn.Text = Text
		Set CreateSimpleNode=tn
	End Function

	Private Sub Out(s)
		'Response.Write(s)
		document.write s
'MsgBox Len(s), vbOKOnly, "Length of S"
show.value = s
'MsgBox s, vbOKOnly, "Value of S"
	End Sub
	
	Public Sub Display
		If m_strXml = "" Then
			m_strXml = "<items>" & Nodes.GetXml(0) & "</items>"		
		End If
		
		dim starttime : starttime = timer()
		Out(GetString())
'		Out "<!--" & timer()- starttime& "-->"
		
		'dim t:t=GetString
		'Out timer()- starttime

		'dim starttime : starttime = timer()		
		'Response.ContentType = "text/xml"
		'Response.Write "<?xml version=""1.0"" encoding=""ISO-8859-1"" ?>"
		'Response.Write GetString() 'm_strXml
		'Response.Write "<!--" & timer()-starttime& "-->"
		'Response.End		
	End Sub
	
			
	Public Sub LoadFromDb(strConn,strMenuTable)
		Const strXsl = "db2xml.xsl"
		m_strXml = GetDbXml(strConn,strMenuTable, strXsl)	
	End Sub
	
	Public Sub LoadFromXml(strXml)		
		m_strXml = strXml	
	End Sub
	
	Function IIf(Expression,TruePart,FalsePart)
		If Expression Then IIf = TruePart Else IIf = FalsePart
	End Function
	
	Private Sub Alert(message,isError)
		'Response.Write "<div style='color:"& IIf(isError,"red","purple") &";font-family:batang;'>" & Replace(message,vbCrlf,"<br/>") & "</div>"
		MsgBox message, vbOKOnly, "Message from Alert function"
    End Sub
    
    Private Function GetDbXml(strConn,strMenuTable, strXsl)
		Dim strXml
	
		strXml = GetDataString(strConn,strMenuTable,"__COL","__ROW","__NULL")
		strXml = Replace(strXml,"&","&amp;")
		strXml = Replace(strXml,"<","&lt;")
		strXml = Replace(strXml,"__COL","</col><col>")
		strXml = Replace(strXml,"__ROW","</col></row><row><col>")
		strXml = Replace(strXml,"__NULL","")	
		
		strXml = "<rows><row><col>" & strXml & "</col></row></rows>"			
		
		strXml = Replace(strXml,"<row><col></col></row>","")
		
		If strXsl <> "" Then		
			Dim xml,xsl
			'Load XML
			if not loadXmlDoc(xml,strXml,"GetDb XML Source") then exit Function
			if not loadXmlDoc(xsl,strXsl,"GetDb XSL Stylesheet") then exit Function
		
			'Transform file
			strXml = xml.transformNode(xsl)
		End If
		
		GetDbXml = strXml        	
	End Function 
	
	Private Function GetDataString(strConn,strMenuTable, ColumnDelimiter, RowDelimiter, NullExpr)
		
		Dim Conn 
		'Set Conn = Server.CreateObject("ADODB.Connection")
		Set Conn = CreateObject("ADODB.Connection")
		Conn.Open strConn
		
		Dim RS
		Set RS = Conn.Execute("SELECT TOP "& MAX_RECORDS & " * FROM " & strMenuTable)
		
		If Not RS.EOF Then
			Dim fld,s		
			
			For Each fld In RS.Fields
				s = s & fld.Name & ColumnDelimiter
			next
			s = s & RowDelimiter
		
			Set fld = Nothing

			'Store our one big string
			GetDataString = s & RS.GetString(,, ColumnDelimiter, RowDelimiter, NullExpr) 

			'Cleanup!
			RS.Close
		End If	
		
		'Cleanup!		
		Set RS = Nothing
		Conn.Close 
		Set Conn = Nothing	
				
	End function
	
	Private Function LoadXmlDoc(ByRef xmldoc, ByVal source, ByVal title)
		If Trim(source) = "" Then
			Call Alert(UCASE(title) &" NOT FOUND",True)
			Exit Function
		End If
		
		'set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
		set xmldoc = CreateObject("Microsoft.XMLDOM")
		xmldoc.async = false
		If InStr(source,"<") Then 
			xmldoc.loadXml(source)
		else
			'If InStr(source,":\") = 0 then source = Server.MapPath(source)
			'test source
			xmldoc.load(source)
		end if
		
		if xmldoc.parseError.errorcode<>0 then
			Call Alert("<b>XML Error:</b><br>" & xmldoc.parseError.reason & "<br><code>"& source & "</code>",True)
		else
			LoadXmlDoc = True
		end if		
	End Function
	
	Private Function GetString()
		Dim xml, xsl
		if not loadXmlDoc(xml,m_strXml,"XML Source") then exit Function
		if not loadXmlDoc(xsl,m_strXsl,"XSL Stylesheet") then exit Function
		
		xml.documentElement.setAttribute "color",Color
		xml.documentElement.setAttribute "imagesfolder",ImagesFolder
		xml.documentElement.setAttribute "defaulttarget",DefaultTarget
		xml.documentElement.setAttribute "id",ID
		
		GetString = xml.transformNode(xsl)
	End Function
	
	Public Sub DisplayFolderContents(ByVal strFolderPath)
		LoadFromFolder(strFolderPath)
		Display
	End Sub
	
	Public Sub LoadFromFolder(ByVal strFolderPath)
		Dim fso 
		'Set fso = Server.CreateObject("Scripting.FileSystemObject")
		Set fso = CreateObject("Scripting.FileSystemObject")
		If fso.FolderExists(strFolderPath) Then			
			Dim strBaseUrl, objBuf
			strBaseUrl = Request.ServerVariables("SCRIPT_NAME")
			strBaseUrl = Left(strBaseUrl,InStrRev(strBaseUrl,"/") - 1)
			Set objBuf = New Buffer
			objBuf.Append "<items>"
			Call LoadFolderContents(objBuf,fso.GetFolder(strFolderPath),0,0)			
			objBuf.Append "</items>"
			m_strXml = objBuf.ToString 
		Else
			Out "<font color=red>Folder <b>'" & strFolderPath & "'</b> does not exist</font>"
		End If
		Set fso = Nothing
	End Sub
	
	Private Sub LoadFolderContents(ByRef objBuf,ByRef objFolder,ByRef intCounter,ByVal intParentId)
		Dim objSubFolder, objFile		
				 
		For Each objSubFolder In objFolder.SubFolders
			
			intCounter = intCounter + 1
			
			objBuf.Append "<item>"
			objBuf.Append("<menuid>" & intCounter & "</menuid>")
			objBuf.Append("<text>" & objSubFolder.Name & "</text>")
			objBuf.Append("<parentid>" & intParentId & "</parentid>")
			objBuf.Append("</item>")
			Call LoadFolderContents(objBuf,objSubFolder,intCounter,intCounter)			
		Next
		
		For Each objFile In objFolder.Files
			intCounter = intCounter + 1
			objBuf.Append "<item>"
			objBuf.Append("<menuid>" & intCounter & "</menuid>")
			objBuf.Append("<text>" & objFile.Name & "</text>")
			objBuf.Append("<parentid>" & intParentId & "</parentid>")
			objBuf.Append("</item>")			
		Next		
		
		Set objFile = Nothing
		Set objSubFolder = Nothing
	End Sub
End Class

This is the treeview class that actually builds the string for output, and this is the code in the .hta file that uses the class:

Code:
<script type="text/vbscript">
<!--script

'declare objects
Dim objTV, strFilePath

'set your file path
strFilePath = "info.xml" 
'create instances of your treeview
Set objTV = New TreeView

'load the treeview from the xml file
Call objTV.LoadFromXml(strFilePath)

'display your treeview 
objTV.Display 

'clear memory 
Set objTV = Nothing
-->
</script>

Now the code is reading from an xml file that a script that I wrote, but I'm not sure the xml file will do much good. And I'm not sure if the xsl file will do much good either.
 
Could it be the / character that causes the problem in the .write?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top