xyz
xyz

Reputation: 2282

<script> inside response from Ajax.Request

is there a possibility to update innerHTML of element by Ajax.request that include tags ?

<div><script type="text/javascript">javascript</script></div>

I would like to update content of div with code from top, not eval it.

Thanks for help.

EDIT:

Ok, so it's not as easy as should be.

I have main document that use Ajax.request that returns form, how to use prototype function from main document to manipulate data after form loads ?

EDIT:

This is button from Ajax.request response

<button onclick="sendForm();" id="submitForm" type="button" class="button"><span><?php echo $this->__('Send email') ?></span></button>

This if JS from main document

new Ajax.Request('/path/another_path/index',
      {
        method:'post',
        evalJS: true,
        onSuccess: function(transport){
            $('sendfriend').update(transport.responseText);
            var productSendtofriendForm = new VarienForm('product_sendtofriend_form');
        }
      });

And after clicking on this button i get "ReferenceError: Can't find variable: productSendtofriendForm"

Upvotes: 0

Views: 1715

Answers (3)

xyz
xyz

Reputation: 2282

Ok, sorry.

Only thing that i needed to do was to declare var productSendtofriendForm before Ajax.Request... needed to bo global one.

Upvotes: 0

clockworkgeek
clockworkgeek

Reputation: 37700

Since you're updating the contents of an existing element you can both make use of Ajax.Updater and set it's evalScripts option. The script tag that gets inserted will be evaluated in the scope of the Ajax.Updater, meaning it can access functions that are global but not ones that are local to the script which started the update.

Edit:
You want a function contained in the script tag to exist in global scope. Instead of this:

function somefunc() { ...

You need to do it this way:

somefunc = function() { ...

And definitely don't use var as that prevents somefunc becoming a global variable. Later, when a button is clicked it's onclick event executes in global scope where somefunc now resides.

Upvotes: 1

gertas
gertas

Reputation: 17145

Nice article about experiments with innerHtml and script tags. It shouln't work but if SCRIPT tag has DEFER attribute then it will work under IE.

Upvotes: 0

Related Questions