gpt4 book ai didi

javascript - EVAL 危险 如何更换

转载 作者:行者123 更新时间:2023-11-28 15:01:28 27 4
gpt4 key购买 nike

我有以下弹出窗口代码(客户端请求)。它使用 eval ,我知道这是危险的。有没有办法重写下面的脚本,使其不使用 (eval)?

/* exported popup_default , popup_help , popup_sitemap , popup_footerlinks */

var matchClass = ['popup_default', 'popup_sitemap', 'popup_footerlinks', 'popup_help'];
var newwindow = ''

var popup_default = 'width=800,height=640,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=250,top=200';
var popup_help = 'width=700,height=650,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=100,top=100';
var popup_sitemap = 'width=1000,height=600,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=100,top=100';
var popup_footerlinks = 'width=800,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=250,top=200';

function pop_ups() {
"use strict";
var x = 0;
var popClass;
while (x < matchClass.length) {
popClass = "'." + matchClass[x] + "'";
$(eval(popClass)).click(function() {
var popurl = $(this).attr('href');
var popupSpecs = $(this).attr('class');
var popupName = Math.floor(Math.random() * 10000001);
newwindow = window.open(popurl, popupName, eval(popupSpecs));
return false;
});
x++;
}
}

$(function() {
"use strict";
pop_ups();
});

最佳答案

你会想使用

"use strict";
function makePopup(className, specs) {
$('.'+className).click(function(e) {
e.preventDefault();
var popupName = Math.floor(Math.random()*10000001);
window.open(this.href, popupName, specs);
});
}
var popups = {
popup_default: 'width=800,height=640,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=250,top=200',
popup_help: 'width=700,height=650,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=100,top=100',
popup_sitemap: 'width=1000,height=600,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=100,top=100',
popup_footerlinks: 'width=800,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=250,top=200'
};
$(function() {
for (var key in popups)
makePopup(key, popups[key]);
});

不用第一个eval,只需使用字符串连接。使用对象来查找属性名称,而不是第二个 eval

关于javascript - EVAL 危险 如何更换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40777013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com