Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Anyone know how to use JavaScript variable with an #include statement

I can't seem to get the syntax right, if it's even possible


What I have tried:

<div id="vcontainer" runat="server" >
 <script type="text/javascript">
//var srcFile = getmmView('<%# Container.EvalDataItem("FileName_wo_ext") %>');
      var srcFile = "~/Views/D1C1.html"  <<above function returns a string like this

// this works <!--#include virtual="~/Views/D1C1.html" -->
//this doesn't  <!--#include virtual=+srcFile+ -->
//this doesn't  document.write("<!--#include virtual='"+srcFile+ "'-->")

Updated 20-Feb-17 13:15pm
ZurdoDev 20-Feb-17 11:36am    
If you have something that works, what is your question?
Karthik_Mahalingam 20-Feb-17 13:59pm    
GAT_TECH 20-Feb-17 19:12pm    
hello Karthik
the code is actually in a databound control template, and I send a value to a javascript function that returns a value to my javascript variable, that I then want to use with include statement, I just put the example there with the static string to test that the include works with a const string,
sorry for not being clear in original post.

var srcFile = getmmView('<%# Container.EvalDataItem("FileName_wo_ext") %>');
Karthik_Mahalingam 20-Feb-17 20:03pm    
I am not sure about it, sorry gat333

1 solution

What you're trying to do is not possible. #include is processed on the server, whereas Javascript is processed on the client, after your page has executed and sent the response.

You can't use code running on the client to modify what happened on the server to generate the page containing the client-side code!

You'll need to find a different way to achieve your requirements. For example, you could use jQuery's .load()[^] or ajax()[^] methods to load content from the server on demand.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900