1

I'm using update panel, my response have some javascript like bellow. After a success response, I need to eval it, load it (with external script)

ex: my html response

<script type="text/javascript" src="test.js"></script>
<script type="text/javascript">
alert('asd');
</script>
<div>test</div>
<div>blah blah blah</div>
Cœur
  • 37,241
  • 25
  • 195
  • 267
complez
  • 7,882
  • 11
  • 48
  • 56
  • JavaScript doesn't get executed when an UpdatePanel updates. You'll need to revise your approach. – Noon Silk Jan 07 '10 at 04:49
  • 2
    Please clarify your question... it doesn't make sense as it's currently worded... – John Weldon Jan 07 '10 at 04:49
  • umm... you want something to happen when the the user clicks the ok on the alert()? I'd say what you really want is an `` here. – Hogan Jan 07 '10 at 05:03

1 Answers1

5

I'm not sure whether this question is still important for you, however I will try to provide a reasonable answer below.

AJAX framework does not evaluate scripts which are returned via UpdatePanel calls. You have to re-attach external scripts to document, so that browser could request for them and evaluate all inline scripts. You can use a small module I have paste below.

var UpdatePanelEnhancer = function ()
{
    var divSelector = '#liveArea';
    function evaluateScripts()
    {
        $(divSelector).find('script').each(function ()
        {
            if (this.src)
            {
                $('body').append(this);
            }
            else
            {
                var content = $(this).html();
                eval(content);
            }
        });
    };

    Sys.Application.add_load(evaluateScripts);
} ();

The weak part of it is that you have to provide a selector for the element where module should look for a scripts to evaluate ('liveArea' in example), although you can extend the module and provide some cinfiguration to it. Also, I would strongly recommend you to load external javascripts before. If you cannot do it for some reason you should additionally check whether script is already referenced or not to avoid necessary calls and potentially unexpected behaviors and errors .

Damian
  • 511
  • 5
  • 10