thanhlong203 / closure-library

Automatically exported from code.google.com/p/closure-library
0 stars 0 forks source link

goog.ui.Dialog().setContent(content) doesn't run inline script provided in content #170

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I load my dialog content (some forms) via ajax, and the returned ajax response 
includes some inline scripting (form validation). However, if I use 
.setContent() to set the dialog content to my ajax response, the inline script 
isn't run. So I have to set the content to an empty div and then use jQuery 
.append() to inject the content into this div.

It would be much cleaner if .setContent() would also run inline script just 
like jQuery .append(). I see no reason why it shouldn't.

Original issue reported on code.google.com by hannes.ryden@gmail.com on 18 Jun 2010 at 2:25

GoogleCodeExporter commented 8 years ago
Running the scripts inside the AJAX response would require calling eval() which 
is discouraged and is not fully supported by the compiler. I also suspect that 
mixing the HTML with scripts have security implications.

I suggest keep the form validation code inside your JS binary and attach it to 
the DOM when you replace the dialog's content.

Original comment by pall...@google.com on 18 Jun 2010 at 11:45

GoogleCodeExporter commented 8 years ago
Thanks for your response.

I don't know how jQuery does it, but considering they run scripts by default in 
both their .append() and .html() functions I'm not convinced that we can simply 
dismiss this as "bad practice". Not being fully supported by the compiler is of 
course a valid point.

The reason I chose inline scripting was because I wanted a close connection to 
the html form implementation, because the script was very small, and because I 
wanted to avoid another http request for such a small and content-specific 
script. The general form validation library and settings is of course loaded as 
an external library together with other larger scripts.

I will reconsider placing the code in a separate js-file, because I guess it 
can be cached by the browser which would avoid the additional http request, but 
I still think you should consider the possibility of adding script support to 
Closure functions that add html content, considering the latest version of 
jQuery does this.

Thanks.

Original comment by hannes.ryden@gmail.com on 18 Jun 2010 at 2:30