Turn on your headlights and reach up under the dash and wiggle the headlight switch connector. I have had this problem on both my XJ's in the past. You may need to replace the connector, headlight switch or both, or maybe just clean the contacts (and use some dielectric grease). If you've done ANY kind of headlight upgrade (even just getting better bulbs), you may need to install relays so that the lights run through it instead of the switch itself. The stock wiring isn't really that great and wasn't made to run better bulbs that we have today (think Silver Star over stock).