不需要相对路径用   "/"
-----------------------------
只对在IIS建成网站才有效果,如果我是建的虚拟目录HRS,"/"它就会去取http://localhost/,而不是http://localhost/HRS

解决方案 »

  1.   

    alert( document.URL.replace('test.php', '') ) ;这样可以了。用replace的方法来实现。
      

  2.   

    把test.php 这个字符串改成你当前文件相对于根目录的路径就行了
      

  3.   

    johin727(我是新手我怕谁?) 
    你这种方法肯定不行,你可能都还没理解我的意思
    test.php有可能放在网站根目录的第一层目录下或第二层、三层下,你获得它的当有位置没用,我要获得它的根
      

  4.   

    在jsp里:
    <%
    String webRoot = request.getContextPath();
    %>
    <script>
    var webRoot = "<%=webRoot%>";
    </script>在js里就可以用webRoot去根目录了
      

  5.   

    var strFullPath=window.document.location.href;
    var strPath=window.document.location.pathname;
    alert(strPath+"路径名称");
    var pos=strFullPath.indexOf(strPath);
    var prePath=strFullPath.substring(0,pos);
    return prePath;
      

  6.   

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

  7.   


    对于这样一个URL
    http://www.x2y2.com:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere