views:

895

answers:

2

Hi, I'm using Javascript, ASP.net and c#. I want to write a custom control to check if javascript is enabled on the browser and display a message. I'm using the following approach: -the control shows a message "Javascript disable" in a tag -the control adds in the section a javascript section with this line:

window.onload = function() {{document.getElementById('My_MESSAGE_DIV').style.display = 'none';}}

The problem is the following: when Javascipt is enabled the onload event is fired when the page is already displayed so the message "Javascript disable" appears on the page and disappear immediately, I would like to avoid this! Any suggestion?

Possible solution: - find an event that get fired before the document is displayed. Any suggestions?

+3  A: 

Um, how about the noscript tag to display your message? Doesn't always have to be that complex.

annakata
A: 

I vote for annakata's answer. Alternatively - if you want to choose the complex way ;) - where you don't want to just display a message but provide alternative behavior in case of the presence/absence of JavaScript I do something like having two kind of containers (divs, Panels or simple labels for msgs), where the container providing JavaScript functionality is hidden with CSS tags (i.e. display:none). Then I render a JavaScript which on startup will remove the "display:none" from the JavaScript enabled part and add it to the non-javascript part. If JavaScript should not be present, obviously just the non-javascript part will be shown.

I hope I was able to explain myself. But as I mentioned, for simple messages the noscript tag is always preferable. Always strive for the simplest solution;

Juri