js如何获取当前页面所在的路径

JavaScript010

js如何获取当前页面所在的路径,第1张

(1)window.location.href : 整个URl字符串(在浏览器中就是完整的地址栏)返回值: http://www.abc.com/order/index.html?orderid=1&name=java#imhere(2)window.location.protocol :URL 的协议部分返回值:http:(3)window.location.host : URL 的主机部分返回值:www.abc.com(4)window.location.port : URL 的端口部分(如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符)返回值:""(5)window.location.pathname : URL 的路径部分(就是文件地址)返回值:/order/index.html(6)window.location.search : 查询(参数)部分 (除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值)返回值:orderid=1&name=java(7)window.location.hash : 锚点返回值:#imhere(8)document.URL返回值: http://www.abc.com/order/index.html?orderid=1&name=java#imhere//获取Url传过来的值

function Request(name)

{

new RegExp("(^|&)"+name+"=([^&]*)").exec(window.location.search.substr(1))

return RegExp.$2

}

无法获取本地文件的绝对路径,这里涉及到一个浏览器安全问题。

<input type="file" onchang="javascript:changeFile()" />

// 选择文件

function changeFile () {

    var e = this

    // 修正IE8下,文件上传异常

    var files = e.files

    if (files == undefined) {

        return false

    }

    // 取得选择文件相关数据信息

    var file = files[0], fileName = file.name, fileSize = file.size

    // 获取文件后缀名

    var fileType = fileName.substr(fileName.lastIndexOf(".")).toLowerCase()

}