首頁 > web前端 > js教程 > 主體

JS ACTIVEX實作網頁選擇本機目錄路徑對話框_javascript技巧

WBOY
發布: 2016-05-16 17:40:09
原創
1305 人瀏覽過

網頁選擇本機目錄路徑對話框,使用ACTIVEX物件遍歷本機磁碟和目錄,只需網頁啟用相關ACTIVEX的INTERNET選項,對本機瀏覽器的安全性要求不高,簡單易用。

網頁選擇本地目錄路徑對話框範例

1.開啟主頁"select.html",點選「選擇路徑」按鈕進入路徑選擇頁面「dir.html」,選取本機目錄路徑

2.路徑選擇頁面「dir.html」初始化後,載入本機磁碟驅動,然後根據選取的磁碟路徑載入其中的目錄。如目錄中存在子目錄,請雙擊該目錄清單項目進入下一集目錄。

3.點選“確定”,關閉選擇路徑頁面"dir.html",將選取的路徑值回到主頁"select.html"文字方塊上


複製程式碼 程式碼如下:

/**//*
*初始化,將系統所有的磁碟機放入table_drives清單
*/
window.onload = new function init()
{
var fso, s, n, e, x;
fso = new ActiveXObject("Scripting.FileSystemObject");
e = new Enumerator(fso.Drives);
s = "";
for (; !e.atEnd(); e.moveNext())
{
x = e.item();
s = s x.DriveLetter;
s = ":";
if (x.DriveType == 3)
n = x.ShareName;
else if (x.IsReady)
n = x.VolumeName;
else
n = "[磁碟機未就緒]";
s = n ",";
}
var 驅動器 = s.split(",");
var tableDrives = document.getElementById("tables_drives");
for ( var i = 0; i {
var option = document.createElement("OPTION");
drives[i].split(":");
option.value = "["drives[i].split(":")[0] ":]"drives[i].split(":")[1];
option.text = "["drives[i].split(":")[0] ":]"drives[i].split(":")[1];
tableDrives.add(選項);
}
get_drives();
}

/**//*
*tables_drives清單中選取的磁碟機上所有資料夾放入table_folder清單中
*/
function get_drives()
{
var tableDrives = document.getElementById("tables_drives");
var tableFolders = document.getElementById("table_folder");
for ( var i = 0; i {
if ( tableDrives.options[i].selected == true )
{
var fvar 、f、fc、s;
vardrive = tableDrives.options[i].value.split(":")[0].substring(1,tableDrives.options[i].value.split(":") [0].長度);
document.getElementById("backDir").value = 磁碟機 ":\";
fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.DriveExists(drive))
{
d = fso.GetDrive(drive);
if ( d.IsReady )
{
f = fso.GetFolder(d.RootFolder);
fc = new Enumerator(f.SubFolders);
s = "";
for (;!fc.atEnd(); fc.moveNext())
{
s = fc.item();
s = ",";
}

var len = tableFolders.options.length;
while(len >= 0)
{
tableFolders.options.remove(len);
len--;
}
var option = document.createElement("OPTION");
option.value = 磁碟機“:\”;
option.text = 磁碟機“:\”;
tableFolders.add(選項);
var 資料夾 = s.split(",");
for ( j = 0; j {
option = document.createElement("OPTION");
option.value = 資料夾[j];
option.text = 資料夾[j];
tableFolders.add(選項);
}
}
else
{
alert("無法目前內容!")
}
}
else
return false;
}
}
}
function get_fileName()
{

var tableFolders = document.getElementById("table_folder");
for ( var i = 0; i {
if ( tableFolders.options[i].selected == true )
{
var檔夾路徑= tableFolders.options[i].value.substring(0,tableFolders.options[i].value.length);
if (folderpath.charAt(folderpath.length-1) == "\")
{
document.getElementById("backDir").value = 資料夾路徑;
}
else
{
document.getElementById("backDir").value = 資料夾路徑 "\";
}
}
}
}
/**//*
*table_folder 上面選項中的一個選項,就在裝箱資料夾下面的資料夾顯示在table_folder列表中。
*/
function get_file()
{
var tableFolders = document.getElementById("table_folder");
var tableDrives = document.getElementById("tables_drives");
for ( var i = 0; i {
if ( tableFolders.options[i].selected == true )
{
var fso 、f、fc、s;
varfolderpath = tableFolders.options[i].value.substring(0,tableFolders.options[i].value.length);
if (folderpath.charAt(folderpath.length-1) == "\")
{
document.getElementById("backDir").value = 資料夾路徑;
}
else
{
document.getElementById("backDir").value = 資料夾路徑 "\";
}


fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFolder(資料夾路徑);
fc = new Enumerator(f.SubFolders);
s = "";
for (;!fc.atEnd(); fc.moveNext())
{
s = fc.item();
s = ",";
}
var len = tableFolders.options.length;
while(len >= 0)
{
tableFolders.options.remove(len);
len--;
}
var opt = "";
var opt1 = "";
for ( j = 0; j {
var option = document.createElement("OPTION");
opt = optfolderpath.split("\")[j] "\";
if ( j > 0)
{
opt1 = opt;
option.value = opt1.substring(0,opt1.length-1);
option.text = opt1.substring(0,opt1.length-1);
tableFolders.add(選項);
}
其他
{
option.value = opt;
option.text = opt;
tableFolders.add(選項);
}

}
if ( tableFolders.options[0].value == tableFolders.options[1].value )
{
tableFolders.options.remove(1) ;
}
if ( s != "" )
{
var 資料夾 = s.split(",");
for ( j = 0; j {
option = document.createElement("OPTION");
option.value = 資料夾[j];
option.text = 資料夾[j];
tableFolders.add(選項);
}
}
}
}
}
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板