检查javascript是否已启用,如果未使用asp自定义控件则显示消息

时间:2022-11-06 19:03:14

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:

我正在使用Javascript,ASP.net和c#。我想编写一个自定义控件来检查浏览器上是否启用了javascript并显示消息。我正在使用以下方法: - 控件在标记中显示消息“Javascript disable” - 控件在该部分添加了一行javascript部分:

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?

问题如下:当启用Javascipt时,当页面已经显示时会触发onload事件,因此页面上会显示消息“Javascript disable”并立即消失,我想避免这种情况!有什么建议吗?

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

可能的解决方案: - 查找在显示文档之前触发的事件。有什么建议?

2 个解决方案

#1


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

嗯,noscript标签如何显示你的消息?并不总是那么复杂。

#2


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.

我投票给annakata的答案。或者 - 如果你想选择复杂的方式;) - 你不想只显示一条消息但是在存在/不存在JavaScript的情况下提供替代行为我会做类似于两种容器的事情(div,Panels)或msgs的简单标签),其中提供JavaScript功能的容器被CSS标签隐藏(即display:none)。然后我渲染一个JavaScript,它在启动时将从启用JavaScript的部分中删除“display:none”并将其添加到非javascript部分。如果JavaScript不应该出现,显然只会显示非javascript部分。

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;

我希望我能解释自己。但正如我所提到的,对于简单的消息,noscript标签总是更可取的。始终致力于最简单的解决方案;

#1


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

嗯,noscript标签如何显示你的消息?并不总是那么复杂。

#2


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.

我投票给annakata的答案。或者 - 如果你想选择复杂的方式;) - 你不想只显示一条消息但是在存在/不存在JavaScript的情况下提供替代行为我会做类似于两种容器的事情(div,Panels)或msgs的简单标签),其中提供JavaScript功能的容器被CSS标签隐藏(即display:none)。然后我渲染一个JavaScript,它在启动时将从启用JavaScript的部分中删除“display:none”并将其添加到非javascript部分。如果JavaScript不应该出现,显然只会显示非javascript部分。

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;

我希望我能解释自己。但正如我所提到的,对于简单的消息,noscript标签总是更可取的。始终致力于最简单的解决方案;