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

using scripts in html files

Status
Not open for further replies.

marianneb

Programmer
Apr 3, 2003
3
FR
I used to write my previous websites with asp, cause i needed a database. Now, i don't need any database and my new webhosting don't offer ASP. SO, i am writing my pages in full html. But i need to use vbscript (mostly to display the contents of files), but none of the scripts i try works!
Even the simplest:
<html>
<head>
<title> welcome</title>
</head>
<body>
<p>beginning</p>
<SCRIPT LANGUAGE=&quot;VBScript&quot;>
Response.Write(&quot;hello&quot;)
</script>
<p>end</p>
</body>
</html>

I just get my &quot;beginning&quot; and &quot;end&quot;. What's the matter with it ????
Thanks you for your help, I'm sure it's obvious for you!
 
The document model for DHTML varies from browser to browser, and I don't know of any browser besides IE that has native support for VBScript.

If you are coding for IE, you might want to check out the docs at:


In your example you can get away with just changing that snippet of script to:
Code:
document.write &quot;hello&quot;
Even though VBScript is not case-sensitive it is a good idea to use the correct case when &quot;spelling&quot; the names of things. For example, in ASP:
Code:
Response.Write &quot;hello&quot;
... is correct because both
Code:
Response
and
Code:
Write
start with a capital letter. But in IE the
Code:
document
object is spelled all lowercase, as is this object's
Code:
write
method.

Little things matter, as illustrated below:



Did you notice that I removed the parentheses? That is because they are incorrect, and can introduce hard-to-troubleshoot side effects.

In VBScript, if you call a Sub or if you call a Function or method that returns a result that you will discard you do not use parens around the parameter list unless you use the
Code:
Call
keyword:

Code:
strResult = MyFunction(lngParam)
<-- Correct
Code:
Call MyFunction(lngParam)
<-- Correct
Code:
MyFunction(lngParam)
<-- Works, maybe not as intended

While the third example will often work, it can really get you in trouble too. That example is the same as saying:

Code:
Call MyFunction((lngParam))
<-- Works, maybe not as intended

For example if MyFunction's parameter is
Code:
ByRef
and the function is supposed to update the value of the parameter before exiting... you now have a bug.

The extra parens in both red-flagged examples tell VBScript to evaluate the value of the expression between them and create a temporary copy. This copy gets passed to your function, and the function can only update the copy - which gets thrown away when the function returns!

I wish I had a dollar for every ASP page I've helped somebody fix because of this error. And it can mess you up good in DHTML or a WSH script as well. Even when it works it's needlessly wasteful, creating extra copies of things just to throw them away soon after.

Want more proof? What about when you have two or more parameters?

Code:
MySub lngParam, strParam
<-- Correct
Code:
MySub(lngParam, strParam)
<-- Kablooie! Error



Please don't feel singled out. I see this sort of thing constantly. I blame the drastically poor examples floating around on web sites, along with some really poor instructional materials on the market.

Remember: Parentheses are not &quot;just punctuation&quot; like some people have cried (&quot;That can't be it! What else did you change that made it work?&quot;) when I fixed their VBScript by taking out extra ones. We never did find out just what caused the shuttle to burn up, did we? Oops!


Happy scripting!
 
Thanks Dilletante! I fell better now with my &quot;hello&quot; displayed! I use IE, and test thru PWS (windows 98).
But my aim is not only to display hello...Of course as long as it didn't work, no need trying to do anything more!
Well, i want to display the content of a text file which is in the server. Say the name is test.txt. I try with what i used with asp, but??? The problem is :
- how to tell the script to run on the server ?
- i removed any uppercase letter, but is it still correct ?

Thank you.


<html>
<head>
<title>welcome</title>
<script language=&quot;VBScript&quot;>
sub readfiles
dim fso, ts, f_name, f_name_serv
const forreading = 1

document.write &quot;hello&quot;
// this hello works, thanks you!)

f_name = &quot;test.txt&quot;
f_name_serv = server.mappath(f_name)
set fso = server.createobject(&quot;scripting.filesystemobject&quot;)
set ts = fso.opentextfile(f_name_serv, forreading)
document.write(ts.readall)
ts.close

end sub
</script>

</head>
<body>
<p>beginning</p>
<script language=&quot;VBScript&quot;>
call readfiles
</script>
<p>end</p>

</body>
</html>
 
Ahh, well now it gets really interesting...

There isn't any server object to play with here just for starters, and thus no mappath method. Your script runs at the client (in the browser). Even if there were some way to get that filename, you can't read it with FSO. At best (relaxed security settings in the browser, security warning popup dialog and the user says &quot;ok&quot;) you can only read local files (on the client machine) this way.

1. Are you trying to build some sort of &quot;client-side include&quot; function?

2. Is the text file stored as part of your web site (in the root of your web site or a subfolder)?

3. Is it really text, and not HTML?

4. Is it static, only updated when you publish a new text file?

If all of these are &quot;yes&quot; - and for (4.) maybe even if &quot;no&quot; (i.e. it is HTML and not plain text), you just might be able to do this using an IE component called the Tabular Data Control (TDC). TDC (unlike FSO) was designed to be used client-side to fetch a text file from the server, and then let you do what you want with the data at that point. But TDC can't write back to the server.

TDC was intended to be used with data binding, an IE feature you may not be familiar with. The basic idea is that the text file contains tabular data, like a database table, arranged like a CSV data file. The columns (or fields) in the data file get &quot;scooped up&quot; off the server and held in the TDC. Then in your HTML you can define things like <table>s to be data-bound to the TDC. This means IE will build the <table> up from the data in the TDC. You only define things like row templates (<tr>-type rows) and IE repeats the row template as many times as needed to display all the data in the TDC. There is even some cool stuff to automagically page through the data, showing say 10 records at a time and letting the user click on buttons to page forward and back through it - all at the browser.

The basic TDC reference can be found at:


Also see:


For data binding see:


And...


And...



Now what you want to do sounds different. You just want to grab the text (or HTML) from the server and plop it into your page to show it, right?

Well TDC can be &quot;fooled&quot; into doing this too.

More typically you would tell TDC what the field delimiter (normally &quot;,&quot; or vbTab) and record delimiter (normally vbNewLine) are, along with what if anything to treat as &quot;quotes&quot; around text values (normally a &quot;&quot;&quot;&quot; - i.e. a single &quot; a.k.a. Chr(34)).

If your text file has no &quot;|&quot; or &quot;~&quot; in it, tell it the field delimiter is the &quot;|&quot; character and the quote character is &quot;~&quot; and the record delimiter is vbNewLine (default). This means each line of text is fetched into TDC as a single-field row. Then process the rows much like reading lines from a text file via FSO.

If you want to suck in the whole file at once, put some symbol as the very last char of the file, tell TDC this thing is your record delimiter, tell TDC something else bogus is your field delimiter and quote value. This will make it take in the whole file as one, one-field row, with the VBNewLines (CR/LFs) embedded in the value. Then pull out this &quot;field&quot; from this &quot;first row&quot; and do as you wish with the string you get.

I've seen people define the quote to be Chr(&HFD), the field delimiter to be Chr(&HFE), and the row/record/line delimiter to be Chr(&HFF). Then they put a Chr(&HFF) at the end of the file and let the other two weird characters just be there (though not in the text of the file) just to keep TDC happy... keep it from breaking things up into lines and fields. You could use Chr(&HFD) and Chr(&HFE) yourself and then use &quot;|&quot; as the row delimiter. EAsier to get into a text file usually than Chr(&HFF).

You do not have to use TDC with data binding - that's just what it was really made for. See:


That article shows you what doesn't seem to be given in the reference materials (at the links above) on TDC. This secret is that TDC has a recordset property you can use like:
Code:
Dim objRS, strData

Set objRS = objTDC.recordset
objRS.MoveFirst
strData = objRS(0)
Set objRS = Nothing
[code]
This sample assume a TDC called objTDC is already loaded.  It pulls out the value of field 0 of the first record/row in the TDC's recordset, and puts it into strData so we can play with it.  As I said, no data binding is needed - you can mess with the data much as if objRS were a regular ADO Recordset object.

A lot to absorb, but hey!  You trying to get tricky, it takes some trickyness.


Final comment:

I'd really avoid [COLOR=blue]document.write[/color] if possible.  It works, but it can be nasty.  If it does what you want fine, but normally I would declare a named [COLOR=blue]<div>[/color] or [COLOR=blue]<span>[/color] and set either its [COLOR=blue]innerText[/color] or [COLOR=blue]innerHTML[/color] properties.
[code]
  :
<object id=&quot;objTDC&quot; classid=....
  <param name=...
  <param name=...
</object>
  :
  :
<script>
Sub window_onload()
  Dim objRS, strData

  Set objRS = objTDC.recordset
  objRS.MoveFirst
  strData = objRS(0)
  Set objRS = Nothing

  divInclude.innerHTML = strData
End Sub
</script>
  :
  :
<body>
<p>beginning</p>
<div id=&quot;divInclude&quot;>
on page load we put strData here
Code:
</div>
<p>end</p>
</body>
</html>

Good luck! A lot to bite off, hmm?


Anybody else got ideas on this?
 
Here's a working example, I know that looked like a lot to absorb at once:

data.txt
Code:
<span style=&quot;background-color: blue; color: white&quot;>
This is a test of the emergency broadcasting system.<br>
This is only a test.</span>|
data.htm
Code:
<html>
  <head>
    <title>Testing client-side include</title>
    <object id=objTDC
      classid=&quot;clsid:333C7BC4-460F-11D0-BC04-0080C7055A83&quot;>
      <param name=&quot;DataURL&quot; VALUE=&quot;data.txt&quot;>
      <param name=&quot;UseHeader&quot; VALUE=&quot;False&quot;>
      <param name=&quot;FieldDelim&quot; VALUE=&quot;&#FD&quot;>
      <param name=&quot;TextQualifier&quot; VALUE=&quot;&#FE&quot;>
      <param name=&quot;RowDelim&quot; VALUE=&quot;|&quot;>
    </object>
    <script language=&quot;VBScript&quot;>
      Sub window_onload()
        Dim objRS

        Set objRS = objTDC.recordset
        objRS.MoveFirst
        divInclude.innerHTML = objRS(0)
        Set objRS = Nothing
      End Sub
    </script>
  </head>
  <body>
    <p>Beginning</p>
    <div id=&quot;divInclude&quot;></div>
    <p>End</p>
  </body>
</html>
[code]
Copy/paste both of these into files of the appropriate names in some folder on your hard drive.  They don't need to be published to the server.

Double-click on [COLOR=blue]data.htm[/color].

Then try publishing them to a folder on your web server and typing in the URL for [COLOR=blue]data.htm[/color] to show that the client can grab the data from there too.
 
*Sigh*

One last note: In my post (2 posts ago) there was a typo.

After listing 1, 2, 3, 4 questions, the paragraph says:

If all of these are &quot;yes&quot; - and for (4.) maybe even if &quot;no&quot; (i.e. it is HTML and not plain text), you just might be able to do this using an IE component...

It ought to read:

If all of these are &quot;yes&quot; - and for (3.) maybe even if &quot;no&quot; (i.e. it is HTML and not plain text), you just might be able to do this using an IE component...

Sorry.

On top of that question (3.) is moot now anyway. My &quot;working example&quot; (one post ago) makes it clear that HTML as well as plain text can be handled this way.

And finally, note that TDC only works for IE 4.x and later, and even then only if TDC is installed. It almost always is, but local site management (in corporate settings) could have left it out of the IE installs.


Again: Any better ideas out there? This is just how I've done it.
 
whaoo!! I'll print all this stuff, make some tries, and tell you about all this ASAP. I knew i would regret my ASP...
Thanks you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top