-
-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use HTML element with data-attributes instead of JavaScript for rendering shortcodes #124
Comments
The main script does execute on As for the "hidden div (or whatever fits)", are you suggesting that instead of rendering <script> tags just to render DOM nodes with attributes that can be parsed? Something like: <div class="wp-leaflet-map-plugin-node" data-instructions="{...some JSON data here}"> Or are you suggesting something else? If that is what you're suggesting, I love it. Generally, I'd like to have PHP merely create some JSON-input for Javascript. It was an Issue I created awhile back, but closed because it was too vague: #93 I'm not sure if the above DOM example with data attributes would work or not, but I'd love to test it out. |
Exactly like this, the div itself would be hidden... Just one encoded JSON is fine, it might make sence to extract some "key" attributes directly, to make it more obvious. Maybe also, the content of the popup (if any) could be the content of the div, so this must not be encoded as JSON.
Than whenever map was loaded it could load its markers:
An alternative to a div might be a script of type text/plain:
|
is this an issue belonging to mine? I render 1000 markers with the shortcodes and this is really slow... can I put them in one json`? |
@emojized What is your issue? But generally yes, the way it works makes the page slower on load as all markers are evaluated immediately instead of deferred after page load (if i'm not wrong with this). |
so whart should i do then? |
We are using a custom marker shortcode to escape the leaflet shortcode and then load it later. Unfortunately I can't give you the full code, but the idea is as follows:
Then you need a JavaScript based on load hook, like this:
|
Thinking about this some more. I'm concerned about asynchronous sections of the page; the section loads with a map: currently it renders a script tag which directly calls the main script. If we rendered a div, or equivalent, instead, then I'm not sure how the main script will be made aware of it. Does this make sense to you? This makes me wary of making such a change. Does it solve your issue to wrap each script in DOMContentLoaded, like you said in your original post? I'm curious about your current solution involving escaping the code. |
@bozdoz Good point. The main reason for me to use data attributes is not to avoid JavaScript at all, but to improve readability and esp. compatibility with i.e. caching plugins... Ideas:
|
Hello,
The current behavior, directly rendering JavaScript into the content is quite error prone with page builder and other tools.
I.e. I'm using Toolset quite often and can't use the shortcodes within templates as the JavaScript is broken than. I've always workaround this with escaping the code and than executing on DOMContentLoaded (which also improves Caching compatibility btw)
I think it would be better to render a hidden div (or whatever fits) with data attributes and than load it via JavaScript instead.
(beside that it needlessly much JavaScript code printed...)
The text was updated successfully, but these errors were encountered: