homepage Welcome to WebmasterWorld Guest from 107.21.135.68
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
File picker in javascript
converting from VBScript
adni18




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

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




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

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




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

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

Bernard Marx




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

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

gph




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

[msdn.microsoft.com...]

gph




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

Here's some more dialog boxes etc.

Shell Object [msdn.microsoft.com]

adni18




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

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




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

..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;
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved