1

Let say I have the following javascript

function blob(alertText) {
     alert(alertText);
}

I would like to call the function within my view. something like:

<% if (x == 0)
    //Need to Call the javascript function here. e.g. blob("sometext");
%>

How can I call the the javascript function within the enclosed if statement in my view?

4 Answers 4

6

Try:

<% if (x == 0) { %>
    <script type="text/javascript">blob("sometext");</script>
<% } %>

Since the Javascript needs to be put on the page, you need to close off the <% %> tags so that you can print HTML. The specific HTML needs to be a <script> tag that calls the function you need.

Sign up to request clarification or add additional context in comments.

Comments

1

Put a script tag calling the javasript function inside the if statement:

<% if (x == 0) { %>
    <script type="text/javascript">blob("sometext");</script>
<% } %>

Comments

1

I know that you're not using Razor (because of your code sample), but if you want to use it at some time, you can do it like this

@if (x == 0)
{
<text>
    <script type="text/javascript">blob("sometext");</script>
</text>
}

or

@if (x == 0)
{
@:    <script type="text/javascript">blob("sometext");</script>
}

Comments

0
@if (x == 0)
{
    <script type="text/javascript">blob("sometext");</script>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.