js获取文件路径怎么写

JavaScript014

js获取文件路径怎么写,第1张

<script>  

      $("input[name=fileString]").change(function() {  

         //var names = []  

  

         for (var i = 0 i < $(this).get(0).files.length ++i) {  

            // names.push($(this).get(0).files[i].name)  

    //console.log($(this).get(0).files[i].mozFullPath)  

    //方式一:  

    var filePath = $(this).val()  

    console.log(filePath)  

    //方式二:  

    alert($('input[type=file]').val())  

         }  

         //console.log(names)  

//方式三:  

alert($("input[name=fileString]").val())  

  

     })  

       

 </script>

注意:以上方式取到的都是文件的伪路径,由于浏览器的安全策略,Chrome浏览器及Chrome内核的浏览器是不可能获取文件在本地的真实路径的。IE浏览器可以通过设置安全级别,能获取到真实路径。

可以使用window对象的location对象的里的href来获取当前页面的URL地址。

工具原料:编辑器、浏览器

1、Location 对象包含有关当前 URL 的信息,Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问,代码示例如下:

<html>

<head><script type="text/javascript">

<script>

alert(window.location.href) 

</script>

</script>

</head>

<body>

test

</body>

</html>

2、运行的结果如下图: