views:

58

answers:

2

I have to move all my scripts into a separate .js file. But I have wired the code in the client (*.aspx) file, with code such as

<script>
var x=<%=ViewData["Key"];%>
</script>

I'm sure there will be an issue when I move that line to the js file as the server side context can't be accessed.

How do I solve this issue?

+3  A: 

The most straightforward thing to do is to move all JS code except these variable assignments.

Ates Goral
+2  A: 

Effectively, the trick is dependency injection in javascript. First, abstract the variables you are generating from server-side variables into parameters for your javascript methods and objects. Then use a small amount of script in-page to setup the javascripts to run.

If you are dealing with a few rather static things (eg--some path names), another tactic is to create a javascript "configuration" object that is in a separate, server-generated script, that can be called by your other scripts as needed.

Wyatt Barnett
do u mind giving an example .?
vijaysylvester