I use the following code to call some stuff after clicking by an element on a page
var http = require('http');
var fs = require('fs');
var jsdom = require('jsdom');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'});
var document = new jsdom.JSDOM('<!doctype html><html><head></head><body><p>Text</p></body></html>').window.document;
document.getElementsByTagName('p')[0].addEventListener('click', function() {
alert('OK');
});
var page = '<!doctype html><html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>';
res.end(page);
}).listen(80, 'localhost');
But if I click the paragraph, nothing happens
I read the answer by the link https://stackoverflow.com/a/36804251/10587062 but I didn't understand how to resolve my problem
You're hooking the click handler on the server, but you're clicking the element on the client.
Instead, you need to send script code to the client that hooks up the event handler on the DOM element the browser renders, to handle the click and show the alert (which is also a client-side thing). (Which means you don't need JSDom; that's for doing DOM manipulation — like web scraping — server-side.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With