Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个javascript代码做什么?_Javascript - Fatal编程技术网

这个javascript代码做什么?

这个javascript代码做什么?,javascript,Javascript,这段代码将url作为输入,但我不确定它的作用或这样做的好处是什么 var hashPos = url.lastIndexOf ( '#' ); return url.substring( hashPos + 1 ); 它从URL获取哈希值: 因此,如果URL是这样的:http://something.com/foo#xyz,它获取xyz 如果URL中没有哈希标记,则此代码将返回整个URL(可能不是期望的结果) 当没有散列值时,这可能是返回空字符串的更安全的变体: var hashPos = u

这段代码将url作为输入,但我不确定它的作用或这样做的好处是什么

var hashPos = url.lastIndexOf ( '#' );
return url.substring( hashPos + 1 );

它从URL获取哈希值:

因此,如果URL是这样的:
http://something.com/foo#xyz
,它获取
xyz

如果URL中没有哈希标记,则此代码将返回整个URL(可能不是期望的结果)

当没有散列值时,这可能是返回空字符串的更安全的变体:

var hashPos = url.lastIndexOf ( '#' );
if (hashPos != -1) {
    return url.substring( hashPos + 1 );
} else {
    return("");
}

它返回url中
#
后面的内容

详情:

var hashPos = url.lastIndexOf ( '#' ); // Gets the position of the last # found in the string.
return url.substring( hashPos + 1 ); // Gets a piece of the string starting at the given position (hashpos + 1).

它在散列(#)标记后获取URL中的所有内容


返回字符串中从最后一个#字符后面到结尾的部分。i、 在该页面中指定一个位置

var hashPos = url.lastIndexOf ( '#' ); 
这将获取URL字符串中哈希字符(#)的位置

return url.substring( hashPos + 1 ); 
然后返回url字符串中哈希位置之后的所有内容

结果将是散列标签。这在AJAX应用程序中经常使用,您希望保持页面状态,并且能够链接到该状态,而无需实际链接到单独的页面

例如:

var recent_hash = "";                       
setInterval(poll_hash, 100); // Initialize our hash polling interval for ajax urls

function poll_hash() {
 if (url.substring( hashPos + 1 ) == recent_hash) { return; }   // No change
 recent_hash = url.substring( hashPos + 1 );
 process_hash(recent_hash); 
}

function process_hash(hash_id)
{
 var ajax_url = '/ajax/link_hash/' + hash_id;
 $('#some_div').load(ajax_url)
}

具体来说,它返回上次出现在
url
中的
#
之后的任何内容。为了防止sameold不熟悉在url中使用散列标记,需要指出的是,它用于使用元素的name属性为页面中的位置添加书签。导航到页面时,浏览器可以滚动到由哈希标识的内容。
var recent_hash = "";                       
setInterval(poll_hash, 100); // Initialize our hash polling interval for ajax urls

function poll_hash() {
 if (url.substring( hashPos + 1 ) == recent_hash) { return; }   // No change
 recent_hash = url.substring( hashPos + 1 );
 process_hash(recent_hash); 
}

function process_hash(hash_id)
{
 var ajax_url = '/ajax/link_hash/' + hash_id;
 $('#some_div').load(ajax_url)
}