在本文中,我们将学习比较具有相同数据但不同标记的两个 HTML 页面的最佳方法。首先,我们需要知道如何比较两个 HTML 页面。现在,检查两个页面的最有效方法是使用两个页面内容的哈希码并比较它们是否相等。
JavaScript hashCode():为了在 JavaScript 中实现 hashCode(),我们需要创建一个函数来生成散列代码。
现在,哈希码一般由以下实现构成。
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
哈希函数的JavaScript 代码:
Javascript
// Generate a hash code or string
Object.defineProperty(String.prototype, 'hashCode', {
value: function()
{
var hashValue = 0;
var i, code;
for (i = 0; i < this.length; i++)
{
// Returns the unicode of first character
code = this.charCodeAt(i);
hashValue = ((hashValue << 5) - hashValue) + code;
hashValue |= 0;
}
return hashValue;
}
});
HTML
GeeksforGeeks
is a computer science portal that contains articles on mordern
technologies as well as programming.It also helps us to prepare for
various competitive programming competitions as well. So,that's why,
GeeksforGeeks is recommended for every student out there who is eager to
learn about new things related to mordern technology.
GeeksforGeeks
is a computer science portal that contains articles on mordern
technologies as well as programming.It also helps us to prepare for
various competitive programming competitions as well. So,that's why,
GeeksforGeeks is recommended for every
student out there who is eager to learn about new things related to
mordern technology.
HTML
GeeksforGeeks
is a computer science portal that contains articles on mordern
technologies as well as programming.It also helps us to prepare for
various competitive programming competitions as well. So,that's why,
GeeksforGeeks is recommended for every student out there who is eager to
learn about new things related to mordern technology.
GeeksforGeeks
包含(hashValue << 5)的行与 ( hashValue * 31 + char) 相同。所以(hashValue <<5)是hashValue * 32而((hashValue << 5)-hashValue)是hashValue *31。以这种格式编写它只会使其更快并提高效率。
生成函数,我们需要提取两个页面的内容并生成以下哈希码。如果两者匹配,则两者相同,否则它们不同。结果通过使用 JavaScript 的innerHTML()函数的警报消息显示。
HTML代码:
HTML
GeeksforGeeks
is a computer science portal that contains articles on mordern
technologies as well as programming.It also helps us to prepare for
various competitive programming competitions as well. So,that's why,
GeeksforGeeks is recommended for every student out there who is eager to
learn about new things related to mordern technology.
GeeksforGeeks
is a computer science portal that contains articles on mordern
technologies as well as programming.It also helps us to prepare for
various competitive programming competitions as well. So,that's why,
GeeksforGeeks is recommended for every
student out there who is eager to learn about new things related to
mordern technology.
输出:
如果内容不同,则将显示不同的输出。在第二个示例中,使用了更快的方法。
示例 2:
HTML
GeeksforGeeks
is a computer science portal that contains articles on mordern
technologies as well as programming.It also helps us to prepare for
various competitive programming competitions as well. So,that's why,
GeeksforGeeks is recommended for every student out there who is eager to
learn about new things related to mordern technology.
GeeksforGeeks
输出: