Welcome to WebmasterWorld Guest from 54.196.212.62

Forum Moderators: open

Message Too Old, No Replies

File picker in javascript

converting from VBScript

     

adni18

10:34 pm on Jan 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all. I came accross this script for opening a dialog to choose a file, color, font etc. and I was wondering how you can use this in javascript. Does anybody know? Thanks in advance!

<script language="vbscript">
MsgBox BrowseForFile("C:\", "JPEG Images (.jpg)¦*.jpg¦All Files¦*.*")

Function BrowseForFile(pstrPath, pstrFilter)
Set objDialog =

CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = pstrFilter
objDialog.InitialDir = pstrPath
objDialog.Flags = &H80000 + &H4 + &H8
intResult = objDialog.ShowOpen
BrowseForFile = objDialog.FileName
End Function
</script>

Bernard Marx

9:32 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



alert( BrowseForFile("C:\\", "JPEG Images (.jpg)¦*.jpg¦All Files¦*.*") ); // note escaped backslash

We could convert the code in the VBScript block to JScript, but it's not really necessary, as you can keep it in a separate language block / include in your file (whether WSH or HTA). The function is defined, and callable by JS.

It's all OK as long as you actually have the registered COM component, "UserAccounts.CommonDialog".

adni18

1:43 am on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool! Now how do I use that for a color dialog?

Bernard Marx

10:20 am on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know, adni. What color dialog control are you using, and what is its id string?

gph

11:37 pm on Jan 27, 2006 (gmt 0)

gph

11:50 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



Here's some more dialog boxes etc.

Shell Object [msdn.microsoft.com]

adni18

10:28 pm on Feb 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, well, I'm not quite sure what's wrong with this code. Am I missing something important? Thanks!

<OBJECT id=dlgHelper CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" width="0px" height="0px">
</OBJECT>

<script type="text/javascript">

//This variable needs to have global scope for the callColorDlg function to persist the chosen color
var sInitColor = null;
function callColorDlg(){
//if sInitColor is null, the color dialog box has not yet been called
if (sInitColor == null)
var sColor = dlgHelper.ChooseColorDlg();
else
//call the dialog box and initialize the color to the color previously chosen
var sColor = dlgHelper.ChooseColorDlg(sInitColor);
//change the return value from a decimal value to a hex value and make sure the value has 6
//digits to represent the RRGGBB schema required by the color table
sColor = sColor.toString(16);
if (sColor.length < 6) {
var sTempString = "000000".substring(0,6-sColor.length);
sColor = sTempString.concat(sColor);
}
document.execCommand("ForeColor", false, sColor);

//set the initialization color to the color chosen
sInitColor = sColor;

}

callColorDlg();

</script>

Bernard Marx

11:13 pm on Feb 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



..only that some text needs to be selected for the execCommand to work on.
Select some text, and it'll work.

I cut it down a little:

function callColorDlg()
{
// If you want all doc's text:
//uncomment// document.body.createTextRange().select();
// No need for if¦else. Works fine with null value
var sColor = dlgHelper.ChooseColorDlg(sInitColor).toString(16);
sColor = "000000".substring(0,6-sColor.length)+sColor;
document.execCommand("ForeColor", false, sColor);
sInitColor = sColor;
}
 

Featured Threads

Hot Threads This Week

Hot Threads This Month