Hello,
what I am trying to do is to force some arbitrary JavaScript code to execute "inside" a DOM element, e.g. <div>
. In other words, is it possible to make a piece of code "thinking" that <div>
is the root of document hierarchy (or <body>
of a document)?
Real life example:
Let's say we have a page that allows executing JavaScript code:
<!DOCTYPE html>
<html>
<head>
<title>Executor</title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div></div>
<input/>
<button onclick="$('div').html(eval($('input').val()))">
Execute
</button>
</body>
</html>
The code has access to entire document and can modify it, e.g:
$("input").hide()
Will prevent me from writing any stupid code any more ;-)
Is is possible to restrict the area of effect for some piece of JavaScript code? and how to do it if it is possible?
Best regards.
Edit:
The code I want to execute might be everything. If Your solution is based on some kind of modification of the given code, please describe how to do it the right way.
Edit2:
First of all, I do not know why I get a down-vote.
Secondly, the code I will execute is someone else's arbitrary code, not mine.
To make everything clear:
What I am really trying to do is to simulate a browser somehow. Get the content of some web page, and insert in into mine. This is relatively easy when we decided to turn off JavaScript and cut out all places where it might appear, but this is something I cannot do. For now I want to limit only the DOM modification (the malicious code). I will work on redirects and cookies later, when I have a base idea.