call javascript within vb code load function
I’m trying to call Java script code from within the page_load function code, but I don’t know how!!
I’ve added js file to the solution And put in the page
<script type="text/javascript" src="js/hide.js">
// to hide
getElementById("GradesH3").style.display="none";
getElementById("GradesUL").style.display="none";
<开发者_运维知识库;/script>
Now the code where I want to call it is :
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("UsrRole") = "Rector" Then
End If
What you need is:
If Session("UsrRole") = "Rector" Then
Page.ClientScript.RegisterClientScriptInclude("hide", "js/hide.js")
End If
Remove your existing JS code and change the file "hide.js" to contain this code instead:
window.onload = function() {
getElementById("GradesH3").style.display = "none";
getElementById("GradesUL").style.display = "none";
};
Edit: on second thought, more correct way to do that, without using JS at all, it to make GradesH3
and GradesUL
be server side by adding runat="server"
to them then have such code instead:
If Session("UsrRole") = "Rector" Then
GradesH3.Visible = False
GradesUL.Visible = False
End If
You can have any HTML element become server side, for example:
<h3 id="GradesH3" runat="server">I'm header</h3>
There are several way to achieve what you want.
One is to simply output the javascript from your Page_Load
- either directly or into a Literal
control.
Another is to put the javascript statements into a functions and again, in the page load output a script tag calling the function.
Possibly, the best approach it to use server side controls and control their visibility directly, instead of using javascript.
精彩评论