How can I use ASP.NET inline tags inside JavaScript?

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

<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> 
+4
source share
2 answers

Like the ASP.Net part, but you want it inside quotes, for example:

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

Since it is displayed on the page, it will look like this:

 window.location = "/Movie/MyMovie"; 

Outside of quotes, it will look like this:

 window.location = "/Movie/" + MyMovie; //thinks MyMovie is a variable, not true! 
+7
source

Where is your javascript embedded in an aspx template or in a separate file?

If it is in a separate file, then by default it will not work as expected, since the file will not be processed by the ASP.NET processing pipeline.

If it is built-in, how will you have enough of it, although you also need to quote server tags

 <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> 
+2
source

Source: https://habr.com/ru/post/1305390/


All Articles