Using inserted javascript after initial pageload [RESOLVED]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
johnnyv
Forum Newbie
Posts: 3
Joined: Sun Aug 14, 2005 6:39 pm
Location: New Zealand

Using inserted javascript after initial pageload [RESOLVED]

Post 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>
Last edited by johnnyv on Sun Aug 14, 2005 10:23 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

eval() .. but be very careful with it's use, as injection of "malicious" javascript is readily possible.
johnnyv
Forum Newbie
Posts: 3
Joined: Sun Aug 14, 2005 6:39 pm
Location: New Zealand

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

when I've done similar things in the past, the functions all pages involved use are stored in the outer page code.
johnnyv
Forum Newbie
Posts: 3
Joined: Sun Aug 14, 2005 6:39 pm
Location: New Zealand

Post 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.
Post Reply