stackuser
stackuser

Reputation: 672

Calling Javascript function from if statment in HTML

This code is wriiten in VS 2003 -asp.net

in the HTML page, I have a javascript function checkuser() defined which returns boolean. I would like to enclose the following in a call to this function

<A onclick="stageClear(); stageEditor();" href="javascript: void(0);">Add new Stage</A>

I want something like

<%if checkuser() then%> <A onclick="stageClear(); stageEditor();" href="javascript: void(0);">Add new Stage</A> <% end if %>

But I am getting the error checkuser() not defined

Upvotes: 1

Views: 347

Answers (3)

Chris Sobolewski
Chris Sobolewski

Reputation: 12935

You can't mix your back end and your front end code.

<a onclick="clickHandler()" href="javascript:void(0)">Stuff</a>

and the JS:

<script>
    function clickHandler(){
        if(checkuser()){
            //do something
        }else{
            //do something else
        }
    }
</script>

Upvotes: 0

Icarus
Icarus

Reputation: 63966

If you intend to call the checkuser() function inside a server-side code block (as the one you show), you must define the function on the page's code behind, not on the client-side using JavaScript.

In other words, you need to have a function called

protected bool checkuser()
{
    return true;//just an example
}

And then you can call it as you intended to.

Upvotes: 2

Pete
Pete

Reputation: 58432

Your code looks as if it is looking for an asp function called checkuser.

if you wanted to do it with js you would need to do something like this:

<script type="text/javascript">
   if (checkuser()) {
      document.write('<a onclick="stageClear(); stageEditor();" href="javascript: void(0);">Add new Stage</a>');
   }
</script>

Upvotes: 2

Related Questions