Home  >  Article  >  Web Front-end  >  Two solutions for Javascript folder selection box_javascript skills

Two solutions for Javascript folder selection box_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:50:561019browse
Solution 1:
Call the windows shell, but there will be security issues.

* browseFolder.js
* This file defines the BrowseFolder() function, which will provide A folder selection dialog box
* for users to implement the function of selecting system folders
* The starting directory of the folder selection dialog box is determined by
* Shell.BrowseForFolder(WINDOW_HANDLE, Message, OPTIONS, strPath) strPath parameter setting of function
*
* For example: 0x11--My computer
* 0 --Desktop
* "c:\"--System C drive
*
* Use the following code to apply this function to an HTML file:
*
* Or copy the following code directly to tag;

* Special note is that due to security issues, you also need the following settings to make this JS code run correctly ,
* Otherwise, the problem of "no permissions" will occur.
*
* 1. Set up a trusted site (for example, the local one can be: http://localhost)
* 2. Secondly: In the custom settings of trusted site security level: set the following options
* "Initialize and script run ActiveX controls not marked as safe"----"Enable"

browserFolder.js:
Copy code The code is as follows:

/**//***
path The object id to display the value
****/
function browseFolder(path) {
try {
var Message = "u8bf7u9009u62e9u6587u4ef6u5939"; //Select box prompt message
var Shell = new ActiveXObject("Shell.Application");
var Folder = Shell.BrowseForFolder(0, Message, 64, 17); //The starting directory is: My computer
//var Folder = Shell.BrowseForFolder(0,Message,0); //The starting directory is :Desktop
if (Folder != null) {
Folder = Folder.items(); // Return FolderItems object
Folder = Folder.item(); // Return Folderitem object
Folder = Folder.Path; // Return path
if (Folder.charAt(Folder.length - 1) != "\") {
Folder = Folder "\";
}
document.getElementById (path).value = Folder;
return Folder;
}
}
catch (e) {
alert(e.message);
}
}

When used:
Copy code The code is as follows:

< ;td>



value="Select the generation path" />


2. Solution 2:
Write one yourself js reads the selection box of the local hard disk. The disadvantage is that the appearance is worse than the previous one.
Copy code Code As follows:



无标题文档




















文件保存位置:
<%----%>

目录位置:




说明:双击列表框的一个选项,就将该文件夹下面的文件夹显示在该列表框中。第一个就是根目录



<script> <br>/**//* <br>* Initialization, put all the drives in the system into the table_drives list <br>*/ <br>window.onload = new function init() <br>{ <br>var fso, s, n, e, x; <br>fso = new ActiveXObject("Scripting.FileSystemObject"); <br>e = new Enumerator(fso.Drives); <br>s = ""; <br>for (; !e.atEnd(); e.moveNext()) <br>{ <br>x = e.item(); <br>s = s x.DriveLetter; <br>s = ":"; <br>if (x.DriveType == 3) <br>n = x.ShareName; <br>else if (x.IsReady) <br>n = x.VolumeName; <br>else <br>n = "[驱动器未就绪]"; <br>s = n ","; <br>} <br>var drives = s.split(","); <br>var tableDrives = document.getElementById("tables_drives"); <br>for ( var i = 0; i < drives.length-1; i ) <BR>{ <BR>var option = document.createElement("OPTION"); <BR>drives[i].split(":"); <BR>option.value = "[" drives[i].split(":")[0] ":]" drives[i].split(":")[1]; <BR>option.text = "[" drives[i].split(":")[0] ":]" drives[i].split(":")[1]; <BR>tableDrives.add(option); <BR>} <BR>} <br><br>/**//* <BR>*All folders on the drive selected in the tables_drives list are placed in the table_folder list <BR>*/ <BR>function get_drives() <BR>{ <BR>var tableDrives = document.getElementById("tables_drives"); <BR>var tableFolders = document.getElementById("table_folder"); <BR>for ( var i = 0; i < tableDrives.options.length; i ) <BR>{ <BR>if ( tableDrives.options[i].selected == true ) <BR>{ <BR>var fso, f, fc, s; <BR>var drive = tableDrives.options[i].value.split(":")[0].substring(1,tableDrives.options[i].value.split(":")[0].length); <BR>document.getElementById("backDir").value = drive ":\"; <BR>fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>if (fso.DriveExists(drive)) <BR>{ <BR>d = fso.GetDrive(drive); <BR>if ( d.IsReady ) <BR>{ <BR>f = fso.GetFolder(d.RootFolder); <BR>fc = new Enumerator(f.SubFolders); <BR>s = ""; <BR>for (;!fc.atEnd(); fc.moveNext()) <BR>{ <BR>s = fc.item(); <BR>s = ","; <BR>} <br><br>var len = tableFolders.options.length; <BR>while(len >= 0) <br>{ <br>tableFolders.options.remove(len); <br>len--; <br>} <br>var option = document.createElement("OPTION"); <br>option.value = drive ":\"; <br>option.text = drive ":\"; <br>tableFolders.add(option); <br>var folders = s.split(","); <br>for ( j = 0; j < folders.length -1; j ) <BR>{ <BR>option = document.createElement("OPTION"); <BR>option.value = folders[j]; <BR>option.text = folders[j]; <BR>tableFolders.add(option); <BR>} <BR>} <BR>else <BR>{ <BR>alert("无法改变当前内容!") <BR>} <BR>} <BR>else <BR>return false; <BR>} <BR>} <BR>} <br><br>/**//* <BR>*table_folder双击选项中的一个选项,就将该文件夹下面的文件夹显示在table_folder列表中。<BR>*/ <BR>function get_file() <BR>{ <BR>var tableFolders = document.getElementById("table_folder"); <BR>var tableDrives = document.getElementById("tables_drives"); <BR>for ( var i = 0; i < tableFolders.options.length; i ) <BR>{ <BR>if ( tableFolders.options[i].selected == true ) <BR>{ <BR>var fso, f, fc, s; <BR>var folderpath = tableFolders.options[i].value.substring(0,tableFolders.options[i].value.length); <BR>if ( folderpath.charAt(folderpath.length-1) == "\" ) <BR>{ <BR>document.getElementById("backDir").value = folderpath; <BR>} <BR>else <BR>{ <BR>document.getElementById("backDir").value = folderpath "\"; <BR>} <br><br><BR>fso = new ActiveXObject("Scripting.FileSystemObject"); <BR>f = fso.GetFolder(folderpath); <BR>fc = new Enumerator(f.SubFolders); <BR>s = ""; <BR>for (;!fc.atEnd(); fc.moveNext()) <BR>{ <BR>s = fc.item(); <BR>s = ","; <BR>} <BR>var len = tableFolders.options.length; <BR>while(len >= 0) <br>{ <br>tableFolders.options.remove(len); <br>len--; <br>} <br>var opt = ""; <br>var opt1 = ""; <br>for ( j = 0; j < folderpath.split("\").length; j ) <BR>{ <BR>var option = document.createElement("OPTION"); <BR>opt = opt folderpath.split("\")[j] "\"; <BR>if ( j > 0) <br>{ <br>opt1 = opt; <br>option.value = opt1.substring(0,opt1.length-1); <br>option.text = opt1.substring(0,opt1.length-1); <br>tableFolders.add(option); <br>} <br>else <br>{ <br>option.value = opt; <br>option.text = opt; <br>tableFolders.add(option); <br>} <br><br>} <br>if ( tableFolders.options[0].value == tableFolders.options[1].value ) <br>{ <br>tableFolders.options.remove(1); <br>} <br>if ( s != "" ) <br>{ <br>var folders = s.split(","); <br>for ( j = 0; j < folders.length -1; j ) <br>{ <br>option = document.createElement("OPTION"); <br>option.value = folders[j]; <br>option.text = folders[j]; <br>tableFolders.add(option); <br>} <br>} <br>} <br>} <br>} <br></script>

如果您还有好的解决方案,回帖分享一下吧.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn