The short answer is yes it’s doable, the long answer is it’s not easy to do it right.
Each browser behaves differently, this includes desktops, mobiles, tablets etc. You’re going to have to be pretty vigilant to code something up that checks the browser type, executes the right variant of code and triggers the message.
I have to be honest and say I don’t use this because I don’t think it’s user friendly and it annoys people.
The best example I can find you to work with is from coderwall.com and the link is below to get you started. It also explains in a little more detail why its not as simple as it used to be!
I would urge you only to use this to promote a discount or one time offer or to make sure the user doesn’t lose a half completed form by accident. A lot of people are using chrome extensions and various tools to stop these from working now and the more widely they are used the less we, as developers and marketers, will be able to leverage this as a tool.
Hope that helps,