refactor javascript to work on all pages