Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Javascript

Status
Not open for further replies.

tsdragon

Programmer
Dec 18, 2000
5,133
US
Is there a simple way to include javascript code with HTML in a string (read from a database and returned via AJAX-type request) that is assigned to a div's innerHTML property and have the javascript evaluated?

Currently I'm using regular expression to parse out the javascript part and eval'ing it separately, but this is cumbersome at best.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
I'm not sure you can do this any more effectively with a single request. You might be able to get rid of the regexp (replacing it with a split, or other search and replace technique)... but you're still going to be left with the eval.

What is the JS in question? If it's not complex code, or a complex expression, etc, you might be able to get away without an eval, but it really depends on the circumstances.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
In this particular case, the javascript consists of some assignment statements to load images into variables, and an image swap function that is called onmouseover or onclick. Its all very simple, but the browser apparently doesn't do anything with it when you insert it into a div's innerHTML.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
They are not things I usually deal with but would loading the content into an iframe work better for this situation so that the page renders directly rather than being parsed and eval'd to get the javascript started?
Iframe's have their own disadvantages so it's usefullness really depends on your need.

At my age I still learn something new every day, but I forget two others.
 
Whoops! Sorry, I just realized you had said the code was read from the database via AJAX so the iframe idea would not be a simple solution.

At my age I still learn something new every day, but I forget two others.
 
Placing the lines of js with a function and calling it doesn't work because the function never gets defined (since the javascript is not evaluated when assigned to the innerHTML property). As far as I've been able to determine, the js is simply ignored.

I'm guessing that there isn't really any other way to work this than the way I'm already doing it, which works OK so I'll just leave it the way it is.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top