A Junub is permitted to pass through the Masjid without staying in it. Allah (Exalted be He) says:
...nor when you are in a state of Janâba, (i.e. in a state of sexual impurity and have not yet taken a bath) except when travelling on the road (without enough water, or just passing through a mosque), till you wash your whole body.
May Allah grant us success. May peace and blessings be upon our Prophet Muhammad, his family, and Companions.