edit: ok I see I cannot post code here for some reason (bubble taking no-code too serious? XD), screenshot attached
I have to put some custom html in a page dynamically, data changes on every pageload, it is for embedding a chat. Using Page HTML Header field for this would be ideal, however I can use HTML element as well.
The embedded code itself is working and verified. It should inject a chat into a website. Works fine on any other hosting. In my app it is a lottery with 20% win rate. Turning off plugins does not solve the problem.
I have an alternative, simpler version of this script that do the same but imports the second from outside
This is working well in my app but I need the first one. I guess the breaking difference is calling a javascript method explicitly.
Thank you very much for your answer @mishav . It is clearly a timing issue. I have noticed it strongly depends on the content of scripts I want to embed and site loading time.
The core functionality of my app is to allow a user to inject his own elements into a page. Those elements are first saved into DB and retrieved when displaying a page, I have ācurrent page thingā as a source for injected HTML (mostly JavaScript). On a page Iām talking about I have just 3 important elements: iframe with external site loaded, a custom html for script injection, my heading.
I think I canāt use ācheck id loadedā method from your link because I do not know what users will inject and what resulting DOM will be ā how many divs etc. and what idās. I have tried this method waiting for a main content to load, but it doesnāt solve the problem. I need some method of ensuring that a given HTML element with custom JavaScript will be the last one to evaluate after all other things. If you have any ideas how to implement this, it would be a great help.
So, the problem is sort-of solved for now, at least in this particular case.
The fix is to place all external elements on a page into a single HTML element and check the ādisplay as iframeā option. Suddenly, everything loads correctly and in order.
It would be great to have a dedicated plugin to ensure that all custom JavaScript loads properly, with the correct dependencies.
I will not mark this post as solved for a while, in the hope of finding a ārealā solution.
The iframe idea is good, and will solve other problems with conflicting css and different DOM paths.
If you know all the dependencies apart from the injected HTML, how about controlling when to inject?
For example,
Page loaded workflow,
Run JS action
param1 set to Search for html to inject's first item's htmltext
script set to
var iii = setInterval(() => {
if (document.getElementById("hypertranscript") &&
document.getElementById("hyperplayer") &&
someLibrary?.someMethod) {
clearInterval(iii);
// inject the html, there are many ways to do this, for example
document.getElementById("parentElement").innerHTML = properties.param1;
}
},200);