Hei guys, i'm trying to simulate ctrl+w on click on image. So i have:
<img id="target" src="something.png"/>
And i try to add a click event on this image which call a function that simulate ctrl+w from keyboard.
I thought it should be something like this:
function closeWindow(){
var theObject = document.getElementById("target");
var pressEvent = document.createEvent ("KeyboardEvent");
pressEvent.initKeyEvent("keypress", true, true, window, true, false, false, false, 87, 0);
theObject.dispatchEvent(pressEvent);
}
The simulated ctrl+w should have the same event as ctrl+w pressed by user from keyboard, i only need to close the tab by pressing a button/image.
AND NO, i cant use window.close();
as this method doesnt work if the page wasnt created by script.
Where am i wrong?