Forum Moderators: open
Simple workarounds (works in all browsers):
Use
eval(). It is way underrated. For code which manipulates DOM -- don't output script code which modifies DOM or manipulates elements. Just modify DOM or manipulate elements directly. Either parse your returned code, or (again)
eval() it. [edited by: DrDoc at 7:32 am (utc) on July 11, 2007]
Thanks DrDoc for the tips... but any short example would be appreciated.
Especially for the eval()... underrated since I rarely if not never use it :)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>JS Test</title>
<style type="text/css">
#foo {
border: 1px solid #c00;
border-left: none;
margin-left: 3.2em;
}
</style>
<script type="text/javascript">
function fakeAJAX() {
return "alert('Hi');";
}
function insertCode() {
elem = document.getElementById('foo');
elem.innerHTML = '<script type="text/javascript">' + fakeAJAX() + '<' + '/script>';
elem.innerHTML = ' ';
}
function evalCode() {
eval(fakeAJAX());
}
function cheat1Code() {
elem = document.getElementById('foo');
elem.innerHTML = ' <img src="http://www.google.com/intl/en_ALL/images/logo.gif" onload="' + fakeAJAX() + '" style="display: none;">';
}
function cheat2Code() {
elem = document.getElementById('foo');
elem.innerHTML = ' <iframe style="display: none;" src="javascript: document.write(\'<script>' + fakeAJAX().replace(/\'/g,"\\\'") + '<' + '/script>\')"></iframe>';
}
</script>
</head>
<body>
<code> <div></code><br>
<div id="foo"> </div>
<code></div></code><br>
<br>
<p>For each of these, if it works, you will get an alert "Hi".</p>
<a href="#" onclick="insertCode(); return false;">Insert code into <div> ...</a><br>
<a href="#" onclick="evalCode(); return false;"><code>eval()</code> me!</a><br>
<a href="#" onclick="cheat1Code(); return false;">Cheat #1</a><br>
<a href="#" onclick="cheat2Code(); return false;">Cheat #2</a><br>
</body>
</html> The last two are cheap ... but work :)
And, you are obviously correct. This is not a real AJAX request. However, the principle is the same. The
fakeAJAX function would naturally be replaced by your standard AJAX functionality. The example above just goes to show that it is certainly possible to get JavaScript returned by an AJAX function as a string can be made to work.
o how about that..
well is there a way to get it the register functions that are retrieved dynamically by ajax content
That is what
eval() does. It takes a string and runs it as JS. eg eval("function foo(){alert('bar')}");
foo();
@DrDoc Those last two are XSS attacks! Cheap. Very very cheap.
We are using a banner rotating system that uses iframes. But when I show AdSense I want the banner to be placed outside the iframe and directly in the page.
The way I thought I could solve this was by placing the iframe in a div. When I want to display AdSense I call a JavaScript function within the iframe on the parent page which replaces the innerHTML from the div with the AdSense code. (I hope my explanation isn't to confusing)
Everything works great, but I cannot seem to get the AdSense to be placed and executed in the div.
Any help would be greatly appreciated!