Également disponible en : Français
The technic is quite simple. You just need to use the
execCommand() function available at the
execCommand() function looks like that:
where the parameters are:
the command name to execute (
false, to use the default interface (or not… not supported bu Mozilla at the moment)
some commands need an argument, for example the
insertImagecommand which is waiting for an URL as value.
As its name tells us, it provide to you the ability to execute a command inside the current window. To get a better and complete documentation the MDN article is a good resource.
To give you the essential point of this feature, I will write a minimalist code example. I’ll let you click on the demo link below (which presents a code too) if you want a more concret how-to-use example.
Let’s imagine this HTML:
document.execCommand() succeeded it returns
true, at the opposite if something went wrong (no support or error) it returns
I let you imagine what you can do with that. By the way, take a look at this example:
Share your code on CodePen and in a new comment in that blog to show your results, I’m curious 🙂