views:

66

answers:

4

On my aspx view, I would like to generate javascript where some parts are generated:

before generation:

<script type="text/javascript">
    var A = 'an id';
    var B = "http://www.yahoo.com" + <%= Model.pathname %>;
</script>

After generation:

<script type="text/javascript">
    var A = 'an id';
    var B = "http://www.yahoo.com/videos/index.htm" ;
</script>

is this possible? what options do I have?

A: 

yes, it's entirely possible, javascript is not executed untill way after all of this stuff is rendered, you have pretty much whatever options you can imagine.

Paul Creasey
+2  A: 

I suggest the following code:

<script type="text/javascript">
    var A = 'an id';
    var B = "http://www.yahoo.com&lt;%= Model.pathname %>";
</script>

Maybe the IntelliSense is not completely right in Visual Studio, but it will work.

GvS
holy crap, it actually worked
MedicineMan
Why the shock MedicineMan? This seems similar to what one could do in classic ASP in some ways.
JB King
A: 

Yes this should work fine, just surround the directive with single quotes for example:

<script type="text/javascript">
    var A = 'an id';
    var B = "http://www.yahoo.com" + '<%= Model.pathname %>';
</script>
Simon Fox
A: 

Yes, that's possible.

If the JavaScript code is in your view, then simply doing the: <%= Model.pathname %> would work.

George Stocker