factorise the code and support more old browser