Reputation: 5529
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>
Upvotes: 4
Views: 7695
Reputation: 125538
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>
Upvotes: 2
Reputation: 630637
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!
Upvotes: 7