Snef wrote: > > > Donny Kurnia schreef: >> Snef wrote: >>> When using $.ajax() i encounter some problems. >>> >>> With the ajax I'm calling a script that will return some html. When >>> echoing the returned data, I can see that everything is returned >>> including the javascript tags. >>> >>> When 'inserting' the returned data into a div on the screen, the >>> javascript seems to be executed but i can't see it in the generated >>> source! >>> >>> I cut it all down to some simple insertion of html into a div like >>> this: >>> >>> $("#freediv").html('<h1 id="test">test</h1><script >>> language="javascript" type="text/javascript">alert("Bla");</ >>> script><p>Bla bla</p>'); >>> >>> The html is inserted in #freediv but i can't see the script tags >>> (although alert("Bla") did get executed). >> What are you use for debugging? In Firebug, inserted javascript tag is >> not appear in the html tab, but it's executed. See the XMLHttpRequest in >> Console tab to verify the returned response is what you intended (html >> and the javascript code) >> >>> The real problem is when i try to use some jQuery in the html: >>> >>> $("#freediv").html('<h1 id="test">test</h1><script >>> language="javascript" type="text/javascript">$("#test").click(function >>> (){ alert("Bla"});</script><p>Bla bla</p>'); >>> >>> Isn't this possible at all? >> It's possible. Just don't forget to wait the DOM loaded: >> >> $("#freediv").html('<h1 id="test">test</h1>'+ >> '<script type="text/javascript>'+ >> 'jQuery(function($){'+ >> ' $("#test").click(function(){'+ >> ' alert("Bla");'+ >> ' });'+ >> '});'+ >> '</script>'); >> >> Watch out for parentheses balance. Write it in indented to easily spot >> the missing parentheses. You code above missing ')' after alert; >> >>> I'm using this to create some 'popup' forms (maybe with SimpleModal) >>> and I want to include some javascript into the response that is >>> specific for that form (like validation and other actions). I know i >>> can use liveQuery or even jQuery's live() but then i need to have all >>> the javascript for every used form already in the main document. >>> >>> I hope you understand my problem ;) >>> >>> >> I always use technique like this. My XMLHttpRequest to server will >> return html code and javascript code. It's working well. > > Donny, > > I have correct { } in my code locally ;) was just a typo. > > The problem is that when i use ajax I DO see the correct html in the > response (even when outputting it to firebug with console.debug), but > when inserting it with .html(data) (where data is the actual html/ > javascript), the javascript tags are gone! > > That's why I just tried it with a clean .html(.....) and there is my > problem! >
Yes Snef, it also happened to me. The javascript tag will be gone when injected to existing DOM. But I got the effect, and I can see that it is the correct response in Firebug's Console tab, so it is not a problem for me. As long everything working like I want, I'm satisfied. -- Donny Kurnia http://hantulab.blogspot.com http://www.plurk.com/user/donnykurnia