Load inline javascript in a partial received via Ajax

Hello.

I’m trying to include a specific JS in a partial that is loaded via AJAX.

The use case is: I’m having a custom popup functionality all over the page; thus, i created various popup types that user’s can choose from, and based on what they select, a proper popup partial is received via Ajax and inserted into the page. Now I need a custom Javascript in there too, but don’t want it hardcoded in script tags, but as separate files.

I could probably just add javascript inside /views/partials/ and use @include but that’s not very nice since Javascript should stay in /assets/scripts in it’s own file, for example “forms.js”.

The other thing that came to mind is using something like this (registering the script before in setup.php of course) inside the ajax-received partial:

{{ wp_enqueue_script(‘forms’) }}

But this won’t work cause it’s popups we are talking about, so the page has already loaded and can’t enqueue stuff later.

I noticed Laravel has something like

{!! HTML::script('js/script.js') !!}

is there a similar approach with Sage ?

Of course, I’d still want the JS minified etc., just not part of the main.js. Or would it be better to just leave it as part of main and modify trigger’s to account for dynamically added content ?

Or should I rethink and do it differently perhaps? Any tips appreciated!

I don’t think there’s any function you can use like wp_enqueue_script() in this case.
I think the simplest thing would be to include a script tag within each popup blade view and use asset_path() to load the correct script file. From there you can use some sort of JS event that’s fired when a popup is opened to init your custom JS.