Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14

Thread: JAVA Script

  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JAVA Script

    How to select a folder from html file dialogue box using Java Script, Jsp or Java

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    JScript, Windows MSIE only.
    getFolder

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hi c this (JAVA Script)

    I need a dialogue box like file dialogue box, to select folder. The answer u gave is different.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    There's no built-in HTML tag or script to browse folders. You will have to script it manually.
    And the method that nikkiH mentioned is one of the methods you will need.
    Actually, what you asked for is a complicated script, because aside from all the list of drives, folders, network folders, etc., you need to display them in a tree view.

    The closest you can get without scripting is by using the file input which can browse local files.

    Code:
    <input type="file" name="file1" />

  • #5
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Browse folder

    Hi ,

    Since i need it ASAP. I know that it can be achieved only through script. but can any one help me to get the code ASAP. That is what I want.

  • #6
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    How about this?

    Code:
    function GetFolder(){
    	var fso;
    	fso = new ActiveXObject("Scripting.FileSystemObject")
    	var BIF_EDITBOX = 0x10;
    	var BIF_NEWDIALOGSTYLE = 0x40;
    	var sa
    	sa = new ActiveXObject("Shell.Application");
    	var oF;
    	oF = sa.BrowseForFolder(0, "My Computer:", BIF_EDITBOX || BIF_NEWDIALOGSTYLE);
    	var fi;
    	fi = oF.Items.Item;
    }

  • #7
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Don't quote me, but I think

    sa = new ActiveXObject("Shell.Application");

    gives security problems with WinXP SP2 unless the site is in the trusted site list.
    Not sure.
    If anyone knows for sure, please post.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    I am sure it will give security problems with SP2. Web pages are not supposed to be able to access the local computer unless it is an intranet site and so only the local site should have local access. Microsoft recommend disabling ActiveX in IE running on older operating systems for the same reason.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    gph
    gph is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    161
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is pretty much the same thing TNO posted with some fixes to problems I've come across.

    As already stated, this is only usable offline.

    Code:
    var fso=new ActiveXObject('Scripting.FileSystemObject');
    
    function folder_dialogue(instruction_text){
    var dir=new ActiveXObject('Shell.Application').BrowseForFolder(0,instruction_text,0x0001);
    if(dir){
    dir=dir.ParentFolder?dir.Items().Item().Path:new ActiveXObject('WScript.Shell').SpecialFolders('Desktop');
    if(dir.charAt(dir.length-1)!='\\')dir+='\\';
    if(fso.FolderExists(dir))return dir
    }
    return false
    }
    
    
    alert(folder_dialogue('some text'))
    Here's the MS BrowseForFolder page

  • #10
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thank every ones effort. The thing i need is some thing like this..

    but i need a proper gui. like file dialogue box.. can any one help in this case..

    <html>
    <head>
    <title>Browse Folders</title>
    <script language="JavaScript">

    var currentFolder='';
    function GetDriveList(){
    var fso, obj, n, e, item, arr=[];
    try {
    fso = new ActiveXObject("Scripting.FileSystemObject");
    } catch(er) {
    alert('Could not load Drives. The ActiveX control could not be started.');
    cancelFolder();
    }

    e = new Enumerator(fso.Drives);
    for(;!e.atEnd();e.moveNext()){
    item = e.item();
    obj = {letter:"",description:""};
    obj.letter = item.DriveLetter;
    if (item.DriveType == 3) obj.description = item.ShareName;
    else if (item.IsReady) obj.description = item.VolumeName;
    else obj.description = "[Drive not ready]";
    arr[arr.length]=obj;
    }
    return(arr);
    }
    function GetSubFolderList(fld){
    var e, arr=[];
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var f = fso.GetFolder(fld.toString());
    var e = new Enumerator(f.SubFolders);
    for(;!e.atEnd();e.moveNext()){
    arr[arr.length]=e.item().Name;
    }
    return(arr);
    }
    function loadDrives(){
    var drives=GetDriveList(),list='';
    for(var i in drives){
    list+='<div onclick="loadList(\''+drives[i].letter+':\\\\\')" class="folders" onmouseover="highlight(this)" onmouseout="unhighlight(this)">'+drives[i].letter+':\\ - '+ drives[i].description+'</div>';
    }
    document.getElementById("path").innerHTML='<a href="" onclick="loadDrives();return false" title="My Computer">My Computer</a>\\';
    document.getElementById("list").innerHTML=list;
    currentFolder='';
    }
    function loadList(fld){
    var path='',list='',paths=fld.split("\\");
    var divPath=document.getElementById("path");
    var divList=document.getElementById("list");
    for(var i=0;i<paths.length-1;i++){
    if(i==paths.length-2){
    path+=paths[i]+' \\';
    }else{
    path+='<a href="" onclick="loadList(\'';
    for(var j=0;j<i+1;j++){
    path+=paths[j]+"\\\\";
    }
    path+='\');return false">'+paths[i]+'</a> \\ ';
    }
    }
    divPath.innerHTML='<a href="" onclick="loadDrives();return false">My Computer</a> \\ '+path;
    divPath.title="My Computer\\"+paths.toString().replace(/,/g,"\\");
    currentFolder=paths.toString().replace(/,/g,"\\");

    var subfolders=GetSubFolderList(fld);
    for(var j=0;j<subfolders.length;j++){
    list+='<div onclick="loadList(\''+(fld+subfolders[j]).replace(/\\/g,"\\\\")+'\\\\\')" onmouseover="highlight(this)" onmouseout="unhighlight(this)" title="'+subfolders[j]+'" class="folders">'+subfolders[j]+"</div>";
    }
    divList.innerHTML=list;
    resizeList();
    divPath.scrollIntoView();
    }
    function resizeList(){
    var divList=document.getElementById("list");
    var divPath=document.getElementById("path");
    if(document.body.clientHeight>0 && divPath.offsetHeight>0){
    divList.style.height=document.body.clientHeight-divPath.scrollHeight;
    }
    }
    function highlight(div){
    div.className="folderButton";
    }
    function unhighlight(div){
    div.className="folders";
    }
    function selectFolder(){
    window.returnValue=currentFolder;
    window.close();
    }
    function cancelFolder(){
    window.returnValue='';
    window.close();
    }

    function GetFolder(){
    alert("hi");
    var fso;
    fso = new ActiveXObject("Scripting.FileSystemObject")
    var BIF_EDITBOX = 0x10;
    var BIF_NEWDIALOGSTYLE = 0x40;
    var sa;
    sa = new ActiveXObject("Shell.Application");
    var oF;
    oF = sa.BrowseForFolder(0, "My Computer:", BIF_EDITBOX || BIF_NEWDIALOGSTYLE);
    var fi;
    fi = oF.Items.Item;
    }
    </script>

    <style>
    #header{
    background-color: #CCCCCC;
    border-bottom: solid 1px black;
    }
    #path{
    position:relative;
    font-size: 8pt;
    font-family: Arial;
    font-weight: bold;
    padding: 2px;
    }
    #list{
    font-size: 10pt;
    font-family: Arial;
    overflow:auto;
    }
    .folders{
    padding: 1px;
    border-top: solid 1px white;
    border-left: solid 1px white;
    border-right: solid 1px white;
    border-bottom: solid 1px black;
    cursor: hand;
    pointer: hand;
    background-color: white;
    }
    .folderButton{
    padding: 0px;
    border-style: outset;
    border-width: 2px;
    border-color:;
    cursor: hand;
    pointer: hand;
    background-color: #CCCCCC;
    }
    A{
    color:blue;
    text-decoration:none;
    padding:3px;
    }
    A:hover{
    background-color: #CCCCCC;
    padding:1px;
    border-style: outset;
    border-width: 2px;
    }
    </style>
    </head>
    <body onload="loadDrives()" onresize="resizeList()" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" scroll=no>
    <form>
    <div id="list">You must allow the ActiveX control to run in order to use this dialog.</div>
    <div id="container">
    <table border="0" cellpadding="0" cellspacing="0" id="header">
    <tr>
    <td align="right" width="1%" nowrap>
    <!-- <input type="button" value="Select" onclick="selectFolder()"> -->
    <input type="button" value="Select" onclick="GetFolder()">
    <input type="button" value="Cancel" onclick="cancelFolder()">
    </td>
    <td><div id="path"></div></td>
    </tr>
    </table>
    </div>
    </form>
    </body>
    </html>

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I modified the sample code from BrowseForFolder page in MSDN that gph linked. The BrowserForFolder() method has already the GUI. I modified it to get the full path of the selected folder.
    Code:
    <script language="JScript">
        function fnShellBrowseForFolderJ()
        {
            var objShell = new ActiveXObject("Shell.Application");
            var ssfCONSTANTS = 17; //ssfDRIVES; see this for more info of constants - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/objects/shell/shellspecialfolderconstants.asp
            var objFolder;
            
            objFolder = objShell.BrowseForFolder(0, "Browse Folder Example", 0, ssfCONSTANTS);
            if (objFolder != null)
            {
    	var fol, path = objFolder.title;
    	var colon = path.indexOf(":");
    	if (colon != -1){
    		path=path.substr(colon-1,2);
    	}
    	else {
    		while (fol = objFolder.parentFolder){
    			colon = fol.title.indexOf(":");
    			if (colon!=-1){
    				path=fol.title.substr(colon-1,2) + "\\" + path;
    				break;
    			}
    			else path = fol.title + "\\" + path;
    			objFolder=objFolder.parentFolder;
    		}
    	}
    	if (path.indexOf("Desktop")!=-1){ //My Computer or Control Panel is selected
    		alert("Please select a drive or a folder.");
    	}
    	else{
    		alert("Selected path: "+path);
    	}
            }
        }

  • #12
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if i use the browsefolder function or fnShellBrowseForFolderJ function i am getting script error. y is it so.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    What error message? Can you post the code you're using?

  • #14
    Registered User
    Join Date
    Oct 2004
    Posts
    592
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by glenngv
    What error message? Can you post the code you're using?
    I also faced one.

    Code:
    
    Object doesn't support this property
    But when I save the files as .hta , they perfectly run.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •