Gleeb
Gleeb

Reputation: 11289

how to process a jsp page with javascript and insert to main page

i am trying to process a jsp page via ajax request and insert it into the main page like this.

$.get('link to page', function(data) { $('#formContainer').html(data); });

in my jsp page that i am getting back from the server there is javascript code that i want to include with the html and will handle the form events (i have many forms and each one has a different functionality)

it seems that the javascript code isn't added to the page with the html code.

Any way to do to what i want?

thanks.

Upvotes: 0

Views: 290

Answers (2)

Michael Lorton
Michael Lorton

Reputation: 44376

Yes, as you've noticed, <script> tags are not executed when you put them into .html(). The only solution I found was to loop through the container, find all the script tags, and execute them by hand.

edit: charlietfl has a superior solution (of which I was previously ignorant). I will leave this post here for anyone who cannot use that solution.

Upvotes: 0

charlietfl
charlietfl

Reputation: 171679

$.get() will strip out script tags ( see docs);

You can use load() which will honor script. You need to place script after the html it affects since document.ready has already occurred in the main page.

$('#formContainer').load('link to page')

API Reference: http://api.jquery.com/load/

Docs explain script execution

Upvotes: 2

Related Questions