在我的网站 www.theprinterdepo.com 中,您可以查看页面源代码,我有一个代码,我的 seo 咨询建议我将其移至外部文件。
它是一个基于 magento 构建的电子商务网站。它是一个免费的开源工具,所以我没有开发它,我只是安装了它。
我需要知道代码的作用。
window.HDUSeed='c7025284683262a8eb81056c48968d74';
window.HDUSeedIntId = setInterval(function(){
if (document.observe) {
document.observe('dom:loaded', function(){
for (var i = 0; i < document.forms.length; i++) {
if (document.forms[i].getAttribute('action') && document.forms[i].getAttribute('action').match('contacts/index/post')) {
var el = document.createElement('input');
el.type = ('hidden');
el.name = 'hdu_seed';
el.value = window.HDUSeed;
document.forms[i].appendChild(el);
}
}
});
clearInterval(window.HDUSeedIntId)
}
}, 100);
最佳答案
简介
此脚本调用 interval 处的函数每 100 毫秒左右 ( as it's not guaranteed ) 尝试验证 DOM 的加载状态以在其上添加一个钩子(Hook)。
如果加载,它会处理页面中存在的所有表单,寻找具有“action”属性的表单(通常将其提交到某个地方,此处 contacts/index/post
)。
对于找到的所有此类表单,它添加了一个包含“种子”值的新隐藏输入元素,但如果不了解更多有关代码库的信息,我们无法告诉您它的用途。
详细代码审查
// seed value, purpose unknown
window.HDUSeed='c7025284683262a8eb81056c48968d74';
// invoke this function every 100ms
// see: https://developer.mozilla.org/en/DOM/window.setInterval
window.HDUSeedIntId = setInterval(function(){
// checks if document.observe method exists (added by the Prototype
// JavaScript library, so we use this here to check its presence or
// that it's been already loaded)
if (document.observe) {
// hook on load status (when the page's DOM has finished loading)
// see: http://www.prototypejs.org/api/document/observe
document.observe('dom:loaded', function(){
// process all forms contained within the page's context
// see: https://developer.mozilla.org/en/DOM/document.forms
for (var i = 0; i < document.forms.length; i++) {
// only act on forms with the 'contacts/index/post/' action attribute
// see: https://developer.mozilla.org/en/DOM/document.forms
// and: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match
if (document.forms[i].getAttribute('action') &&
document.forms[i].getAttribute('action').match('contacts/index/post')) {
// create an element...
// see: https://developer.mozilla.org/en/DOM/document.createElement
var el = document.createElement('input');
el.type = ('hidden'); // ... that is hidden
el.name = 'hdu_seed'; // w/ name 'hdu_seed'
el.value = window.HDUSeed; // and the seed value
document.forms[i].appendChild(el); // and add it to the end of the form
}
}
});
// Remove the interval to not call this stub again,
// as you've done what you want.
// To do this, you call clearInterval with the ID of the
// interval callback you created earlier.
// see: https://developer.mozilla.org/en/DOM/window.clearInterval
clearInterval(window.HDUSeedIntId)
}
}, 100); // 100ms
关于javascript - 复杂的javascript代码,我可以删除它吗?或者它有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10920057/