move the js redirect method and refactor it