这个javascript代码是做什么的?
问题描述:
这个代码是用一个URL作为输入,但我不知道它做什么或什么这样做这个javascript代码是做什么的?
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一切。
var url = "http://www.mysite.com/test#somesection";
var hashPos = url.lastIndexOf ('#');
return url.substring(hashPos + 1); //returns "somesection"
答
返回从最后一个#字符后面到最后一个字符串的部分。即该页面中的位置。
答
var hashPos = url.lastIndexOf ('#');
这会抓取URL字符串中哈希char(#)的位置。
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属性为页面中的位置添加书签是有用的。浏览到页面时,浏览器可以滚动到由散列标识的内容。 – Random