Can I vent a bathroom fan through an attic window?
This could be an option, as long as a few conditions are met.
There should be a damper
A damper will prevent outside air from entering the exhaust pipe, traveling down the length and entering the conditioned space. Most exhaust vent covers are designed in such a way, so that even on windy days the exhaust can still be exhausted.  If you just have a pipe pointing to the outside, it's possible the outside air pressure can overcome the exhaust air pressure preventing the exhaust from exhausting.
There shouldn't be a way for the exhaust to reenter the building
If you simply close the window on a pipe that does not completely fill the opening, it's very likely that air pressure differences will force the exhausted air right back in the window.  You'll want to devise a way to prevent the exhausted air from reentering the building. This other answer illustrates a way to install the vent, while blocking the entire window opening.