从ajax调用jquery解析URL的哈希值

时间:2014-06-05 05:12:59

标签: javascript

让我说我有这个剧本。

$('body').load('someurl.html#12', function(data){
    // my code to proccess data
});

someurl.html中如何获取此网址的哈希值?在这种情况下应该是#12。当我尝试使用window.location.hash解析它时,我得到的是没有哈希的父URL。

1 个答案:

答案 0 :(得分:0)

可以使用jQuery BBQ Full reference from here

完成
  

从网址解析查询字符串或片段

The jQuery.param.querystring and jQuery.param.fragment methods can be used to return a normalized query string or fragment from the current document or a specified URL. Complete usage information is available in the documentation.

// Return the document query string (similar to location.search, but with
// any leading ? stripped out).
var qs = $.param.querystring();

// Return the document fragment (similar to location.hash, but with any
// leading # stripped out. The result is *not* urldecoded).
var hash = $.param.fragment();

// Parse URL, returning the query string, stripping out the leading ?.
// qs is set to "a=1&b=2&c=3"
var qs = $.param.querystring( "/index.php?a=1&b=2&c=3#hello-world" );

// Parse URL, returning the fragment, stripping out the leading #.
// hash is set to "hello-world"
var hash = $.param.fragment( "/index.php?a=1&b=2&c=3#hello-world" );