- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>获取两个文件的相对地址</title>
- <script type="text/javascript">
- function getPath(){
- var filePath_1 = document.getElementById("path_1").value;
- var filePath_2 = document.getElementById("path_2").value;
- if( filePath_1 == filePath_2 )
- {
- document.getElementById("txt_Result").innerHTML = "两个文件时同一个文件";
- }
- else
- {
- var pathArray_1 = new Array();
- var pathArray_2 = new Array();
- var str = "文件2相对于文件1 的位置为:";
- pathArray_1 = filePath_1.split("\\\\");
- pathArray_2 = filePath_2.split("\\\\");
- if( pathArray_1.length >= pathArray_2.length )
- str += "..\\\\" + pathArray_2[ pathArray_2.length - 1 ];
- else
- {
- max_Lenght = pathArray_1.length;
- var start = 0;
- var mark = true;
- while(mark)
- {
- if( pathArray_1[start] == pathArray_2[start] )
- start ++;
- else
- {
- mark = false;
- }
- }
- if(start != 0 )
- str += "..\\\\";
- for(start; start<max_Lenght; start ++ )
- {
- str += pathArray_2[start] + "\\\\";
- }
- str += pathArray_2[max_Lenght];
- }
- //下面是一个测试文件
- /* for(i=0;i< pathArray_1.length;i++)
- {
- str += pathArray_1[i] +"<br />";
- }*/
- document.getElementById("txt_Result").innerHTML = str;
- }
- //document.getElementById("txt_Result").innerHTML = filePath_2;
- }
- </script>
- </head>
- <body>
- <input type="text" id="path_1" value="" />
- <input type="text" id="path_2" value="" />
- <input type="button" id="but" value="提交" onclick="getPath()" />
- <br />
- <span id="txt_Result">
- </span>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/010820134901.html
来源: http://www.codesnippet.cn/detail/010820134901.html