HTML 스크립트
<form method="POST" name="test">
<input type="hidden" name="test2" value="test3">
<input type="submit" name="submit" value="submit" onClick="send()">
</form>
자바 스크립트
function send(){
var form = document.test,
a = eval("form.test2");
alert(a.value); //test3
}
위의 코딩에서 볼 수 있듯이 eval을 사용하여 test2 값을 얻습니다. 하지만 일부 JS 검사기에서는 eval이 허용되지 않습니다. 그래서 어떻게 eval 함수없이 test2 값을 얻을 수 있습니까?
이 정적 예제, 수정 된 코드 시도
var send = function() {
var test2 = document.test.test2.value; //static access without eval
alert(test2);
return false;
}
<form method="post" name="test">
<input type="text" name="test2" value="test3">
<input type="submit" name="submit" value="submit" onclick="return send()">
<form>
이 동적 예제, 수정 된 코드 시도
var find = function(str) {
var arr = str.split('.');
var l = arr.length,
i = 0;
var el = window[arr[i++]]; //<-- we refer to js and html element right from here
while (--l)
el = el[arr[i++]];
return el;
};
var send = function() {
var prop = 'document.test.test2.value'; //<-- string name of the property to access
var el = find(prop);
alert(el);
return false;
}
console.log(find('document')); //sample
console.log(find('alert')); //sample
<form method="post" name="test">
<input type="text" name="test2" value="test3">
<input type="submit" name="submit" value="submit" onclick="return send()">
<form>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다