Pass form to function

Batch, ASP, JScript, Kixtart, etc.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 13 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
tasmant
Posts: 1
Last visit: Wed Nov 03, 2010 8:46 pm

Pass form to function

Post by tasmant »

Hi Guys,

I tried to write a simple hta application. I just wanted to load .ini files in order to build four <select> based on the .ini files.

As the code is the same for each file load, i wanted to write a function in order to reuse it to build each <select>, just passing the form name or id (?)

Part of this code was found on scripting guys blog (or site don't remember), just wanted to improve it ...

I clearly don't arrive to send the <select> into the function.

Code: Select all

<html> 
<head> 
<title>Computer Naming</title> 
 
<HTA:APPLICATION  
    CAPTION="yes" 
    ID="MMD" 
    APPLICATIONNAME="Airbus Naming Computer" 
    BORDER="thin" 
    INNERBORDER="no" 
    SCROLL="no" 
    SINGLEINSTANCE="yes" 
    SHOWINTASKBAR="no" 
    SYSMENU="yes" 
    WINDOWSTATE="normal" 
    SELECTION="no" 
    CONTEXTMENU="no" 
    MAXIMIZEBUTTON="no" 
    MINIMIZEBUTTON="no" 
    NAVIGABLE="yes" 
    ICON="mobsync.exe" 
    VERSION="1.0" 
> 
</head> 
 
<SCRIPT Language="VBScript"> 
 
'#Load each component 
 
'Dim oFile as String 
'Dim oForm as Document.element 
 
Sub Window_Onload 
    LoadForms "sites.ini" ,aSites 
    LoadForms "types.ini" ,aType 
    LoadForms "asset.ini" ,aAsset 
    LoadForms "natco.ini" ,aNatco 
End Sub 
 
'#sites.ini file 
Sub LoadForms(oFile, oForm)  
         
    ClearForm(oForm) 
    oSelect = Document.getElementbyID(oForm) 
    Set objFS   = CreateObject("Scripting.FileSystemObject") 
    Set objFile = objFS.OpenTextFile(oFile) 
    strOptions = objFile.ReadAll 
    objFile.Close 
    arrOptions = Split(strOptions, vbNewLine) 
    For Each strOption in arrOptions 
        Set objOption = Document.createElement("OPTION") 
        If Instr(strOption,"=") = 3 Then 
            objOption.Text = Mid(strOption,4) 
            objOption.Value = Mid(strOption,1,2) 
        Else 
            objOption.Text = Mid(strOption,3) 
            objOption.Value = Mid(strOption,1,1) 
        End If 
        oSelect.Options.Add(objOption) 
        arrOptions = Null 
        strOption = Null 
    Next 
End Sub 
 
'#Refresh on click 
Sub ClearForm(oForm) 
    oSelect = Document.GetElementbyID(oForm) 
    For Each objOption in oSelect.Options 
        objOption.RemoveNode 
    Next  
End Sub 
 
</SCRIPT> 
 
<body> 
    <select onActivate="LoadForms 'sites.ini' ,aSites" id=aSites> 
    </select> 
    <select onActivate="LoadForms 'types.ini' ,aTypes" id=aTypes> 
    </select> 
    <select onActivate="LoadForms 'asset.ini' ,aAsset" id=aAsset> 
    </select> 
    <select onActivate="LoadForms 'natco.ini' ,aNatco" id=aNatco> 
    </select> 
    <p> 
        <input type="text" name="computername"> 
        </input> 
    </p> 
    <div id="display">&nbsp</div> 
</body> 
</html>

Each .ini file attached to this application is write as following :
Parameter=Text to display

Exemple for country:
FR=France
DE=Germany
UK=England
...


Have you any simple idea to do this working ?

Thanks in advance.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Pass form to function

Post by jvierra »

YOu file s not an INI file.

An INI file has the form:

[section1]
key1=value1
[section2]
key1=value1
key2=value2

etc...

With that information can you please explain what your desired outcome is.

It looks like you are trying to do too many things too many ways. Here is a simple way to start.

Code: Select all

<html>
<head>
<title>Computer Naming</title>
</head>
<SCRIPT Language="VBScript">
    Sub LoadForms() 
            
        Set element = window.event.srcElement    
        ClearForm element
        Set fso   = CreateObject("Scripting.FileSystemObject")
        filename = element.id & ".ini"
        MsgBox filename
        Set file = fso.OpenTextFile(filename)
        While Not file.AtEndOfStream
            line = file.ReadLine()
            Set opt = Document.createElement("OPTION")
            a = Split(line,"=")
            opt.Text = a(1)
            opt.Value = a(0)
            element.Options.Add(opt)
        Wend
        
    End Sub
    
    Sub ClearForm(oForm)
        For Each objOption in oForm.Options
            objOption.RemoveNode
        Next 
    End Sub
</SCRIPT>
<body>
    <!-- Naming convention is to use filename as object id that way they are bound together. -->
    <select id="sites" onactivate='LoadForms'>
    </select>
    <select id="types">
    </select>
    <select id="asset">
    </select>
    <select id="natco">
    </select>
    <p>
        <input type="text" name="computername">
        </input>
    </p>
    <div id="display"></div>
</body>
</html>

I recommend not using the INI extension for non-ini files as it can create confusion. INI is a reserved extension for the INI format.

I would design an ini file like this:

Code: Select all

[sites]
	name=value
	....
	[types]
	key=value
	[asset]
	key=value
	[natco]
	key=value.

Unfortunately there are no rewource inVBScript of WSH to read an ini file. You would have t get anini file parser. There are a number available on many web sites that are free or you can write your own. YOu can also just stick to multiple text files.


jvierra2010-11-04 04:43:58
This topic is 13 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked