在JavaScript中获取当前网址?
我正在使用jQuery。 我如何获取当前URL的路径并将其分配给一个变量?
示例网址:
http://localhost/menuname.de?foo=bar&number=0
要获得路径,您可以使用:
var pathname = window.location.pathname; // Returns path only
var url      = window.location.href;     // Returns full URL
在纯jQuery风格:
$(location).attr('href');
位置对象还具有其他属性,如主机,散列,协议和路径名。
http://www.refulz.com:8082/index.php#tab2?foo=789
Property    Result
------------------------------------------
host        www.refulz.com:8082
hostname    www.refulz.com
port        8082
protocol    http:
pathname    index.php
href        http://www.refulz.com:8082/index.php#tab2
hash        #tab2
search      ?foo=789
var x = $(location).attr('<property>');
这只会在你有jQuery的时候才起作用。 例如:
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
</script>
  $(location).attr('href');      // http://www.refulz.com:8082/index.php#tab2
  $(location).attr('pathname');  // index.php
</script>
</html>
                        链接地址: http://www.djcxy.com/p/1891.html
                        
                        
                    