Solved:
took the code in a .js file and included it via prepend to the .html file. Nevertheless: Great forum, many thanks!
Solved:
took the code in a .js file and included it via prepend to the .html file. Nevertheless: Great forum, many thanks!
Hey yariplus,
let me show you:
I wanted to create an onclick-handler to a button, which I now realized by doing this:
var htmlString = fs.readFileSync(FilePath);
var parsedHTML = $.load(htmlString)
parsedHTML('.interactive').attr('onclick', "clickEmit(this.id)").html();
now the clickEmit function is called, when I click any element with the interactive-class.
In the parsed html file now the code is:
<script src='/socket.io/socket.io.js'></script>
<script>
function clickEmit(id){
var socket = io.connect('http://localhost:8080');
socket.emit('buttonClick', id );
}
</script>
My next step would be to take the code above (beginning with <script src='/socket.io/socket.io.js'></script> ), delete it from the .html-file and re-inject it into the file via cheerios prepend-function.
So I now try something like:
var content = "<script src='/socket.io/socket.io.js'></script><script> function clickEmit(id){ ... ... ...} </script>"
parsedHTML('head').prepend(content);
But when I do this, the code appears at the same position as before, but on a click on any button nothing happens anymore. Why is that so?
Greetings and thanks for the good advises!
Hey guys,
I tried cheerio, but as it seems it does not support to inject any kind of handlers to the code. Is there a way to do this or is another alternative needed?
Greetings and thank you!
Hey and thanks for the support
At the end the project serves to create a node.js module, that is used by OTHER people, so I would like to make it fail-save and idiotproof. Does anybody have experience with the named libraries jsdom, domino or cheerio? Or is the shown solution by frissdiegurke the best solution for this?
Sorry, I'm new to node development, especially to the libraries^^
Greetings!
Thats how I did it until now, that does not work when the user does not use <head> but < head > or <head > or anything like that, it's super fragile. Im looking more for something like a DOM-manipulation like in javascript (document.head.appendChild(newLinkTag); ) that wont kill my code if the user changes something.
Hey guys,
im trying to manipulate a .html-file before sending it to the client. Something like entering a <link> element directly after the <head> - tag. Is there a clever way to do this? Until now I did this by using string-operation (like split) which in fact is a horrible way. Does any smart person know a solution to my dilemma?
Greetings!
Ron