4

How can I use ASP.NET inline tags from within a block of JavaScript? For example:

<script type="text/javascript">
     // Do some AJAX here, then redirect to a new page on the next line.
     window.location = "/Movie/" + <%= html.encode(MovieName) %>;
</script>
1
  • Thanks for the responses. I tried putting the inline tags inside the quotes at first, but misinterpreted an error I got. MovieName has its accessibility set to Friend, so it's not usable from ASP.NET. I must have read the error page too quickly at first and assumed I was seeing an error about putting inline tags inside a script block. I should have thoroughly read the error page! Thanks again for the responses. Commented Mar 28, 2010 at 22:25

2 Answers 2

7

Just like you have on the ASP.Net part, but you want it inside the quotes, like this:

window.location = "/Movie/<%= html.encode(MovieName) %>";

Since it echos out to the page, it will render like this:

window.location = "/Movie/MyMovie";

Outside the quotes it would look like this:

window.location = "/Movie/" + MyMovie;
//thinks MyMovie is a variable, not true!
Sign up to request clarification or add additional context in comments.

Comments

2

Where is your JavaScript, inline in the aspx template, or in a separate file?

If it's in a separate file, then by default it will not work as expected as the file will not be subject to the ASP.NET processing pipeline.

If it's inline, then how you have it will suffice, although you need to quote the server tags too

<script type="text/javascript">
     // Do some AJAX here, then redirect to a new page on the next line.
     window.location = "/Movie/<%= html.encode(MovieName) %>";
</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.