Page 1 of 1

Using inserted javascript after initial pageload [RESOLVED]

Posted: Sun Aug 14, 2005 7:01 pm
by johnnyv
Hello i have been googling with no success on this issue.

I have a page that contains a div(id=contents) which i fill with content via a httpXMLrequest object.
To add the response content i use

Code: Select all

document.getElementById('contents').innerHTML = response;
This works fine for normal html and i can use normal javascript functions or previously defined function in the head script, but any script i include in the response will not work.
I have checked the inserted scripts and the syntax is correct.
Is there anyway to make this inserted javascript execute?

here is an example

Code: Select all

<script type="text/javascript">
function inserted_function()
{
alert('inserted function call');
}
</script>
<a href="javascript:inserted_function();\">call inserted function</a>

Posted: Sun Aug 14, 2005 7:10 pm
by feyd
eval() .. but be very careful with it's use, as injection of "malicious" javascript is readily possible.

Posted: Sun Aug 14, 2005 7:28 pm
by johnnyv
Thanks for the reply but im not sure how eval would help me in this case.

I assume you mean passing the return text string through eval()? how does that make the functions then work later on when the
<a href="javascript:inserted_function();\">call inserted function</a> is pressed?

I had already looked at eval() and couldn't figure a way to get it to do what i wanted.

Posted: Sun Aug 14, 2005 8:26 pm
by feyd
when I've done similar things in the past, the functions all pages involved use are stored in the outer page code.

Posted: Sun Aug 14, 2005 9:36 pm
by johnnyv
feyd wrote:when I've done similar things in the past, the functions all pages involved use are stored in the outer page code.
Which is something i am trying to avoid.
However i may have found a solution
http://www.developersdex.com/asp/messag ... &r=4264822

Code: Select all

var oScript=document.createElement("script");
oScript.text=[[variable holding responseText]];
oScript.defer="true";
document.getElementsByTagName("head­")[0].appendChild(oScript);
edit:
It works doing this, but i wanted basically a page shell that could load content without reloading the page.
So i append the scripts to the content div which gets cleared on new content being loaded, works perfectly(and seems much faster that a link to a new page takes to load).

Oh and the oScript.defer="true"; is not needed and probably a bad thing to include.