shortlyportly
Hi All.
I’m developing an app using Phoenix and I want to include a JavaScript library on specific pages (don’t want to load it on those pages that don’t need it). Is there a standard way to achieve this?
cheers
Dave
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
The easiest way is probably to split it into a separate script and put
only on the pages where you want it.
shortlyportly
My fault for no explaining my issue correctly but the library I’m using relies on Jquery which gets loaded at the end of app.html.eex so just including my script in the relevant page doesn’t work as Jquery is loaded yet.
I don’t want to include my script in app.html.eex for all pages as it is rather large and only required for a few pages (for reference it is a graph library and I only have a few pages with graphs - the majority don’t require them).
cheers
Dave
GregFreeman
I’m getting a warning on this webpage (looks like they didn’t update their ssl certificate), but it’s essentially what I’ve been doing:
https://blog.diacode.com/page-specific-javascript-in-phoenix-framework-pt-1
Note I’m not quoting the entire article.
After the MainView is working, you can add a View to every individual subpage that loads page-specific javascript.
This has been my practice for my past few Phoenix apps.
shortlyportly
Thanks Greg - I’ll have a read of the full article.
amnu3387
I was going to post this as I also have been using it but then I noticed someone commented that the app.js still gets the contents of those files concatenated - and I just checked and indeed it seems so - which (kinda…) partially defeats the purpose. If somebody knows how to handle the compilation of js in brunch in order to exclude certain files from the concatenated app.js but still have them be accessible as imports would be great ^
GregFreeman
I’m not sure how to accomplish that.
Where I’ve been using it is to change behavior of the UI in different parts of the app. It hasn’t been after saving bandwidth, as I’ve been doing pretty much everything in vanilla JS or very specific frameworks (like fabric.js).
For example, there are several ways to display a map or other feature and load the other UI features.
You can have an if statement that only uses the relevant parts of the code in app.js. Or you can have that code only execute in separate modules, like the technique in the link.
That was enough to solve my dilemma of a very messy app.js. I couldn’t find an obvious way of doing what you wanted when I originally sought it out a few months ago, or at least anything with a simple search term I could think of. Maybe by doing something with Brunch and Phoenix itself? But, I really don’t know. I saw the second part of that article was about webpack, and that may be required to do something like that. It didn’t seem worth it for what I was doing.
*Edited because the second part is webpack, not jetpack
axelson
A very easy but slightly hacky method is to put
<script src="new_script.js"></script>in the body of the page and then usesetTimeoutin your code until `window.jQuery’ is loaded:From this Stack Overflow answer: https://stackoverflow.com/a/17914854/175830
OvermindDL1
What I do on my polymerized pages is I pass a
extra_javascriptoption to the assigns being a list of what extra javascript to bring in for a page to work, and my primary view just loads those in, in-order, after my app.js as well.But really, get rid of jQuery, it is not useful anymore and if you want the functionality there are shims that are much faster now too.
Foong
You can also use Phoenix.View.render_exiting/3. Check out the “Rendering based on controller template” section in the doc
Max
if String.contains? @conn.request_path, "somepage" doend