'Use dynamic text in Javascript

I use asp.net and this code for open local file with IE:

<script type="text/javascript" language="javascript">
    function RunFile() {
        WshShell = new ActiveXObject("WScript.Shell");
        WshShell.Run("d:/11.doc", 1, false);
    }
</script>


<input type="button" value="Run File" onclick="RunFile();"/>

result is ok :-)

but I want send address local file to script then run this file:

example:

<script type="text/javascript" language="javascript">
    function RunFile() {
        WshShell = new ActiveXObject("WScript.Shell");
        WshShell.Run(**AddressFile**, 1, false);
    }
</script>



protected void Page_Load(object sender, EventArgs e)
{
**AddressFile** = "d:/11.doc";
}


Solution 1:[1]

If you want to call the javascript function dynamically from server-side, simply add a parameter to the JS function to take the file name as so:

<script type="text/javascript" language="javascript">
    function RunFile(fileName) {
        WshShell = new ActiveXObject("WScript.Shell");
        WshShell.Run(fileName, 1, false);
    }
</script>

And then on server-side call the javascript function as so:

this.ClientScript.RegisterStartupScript(this.GetType()
,"somekey"
,"RunFile('"+file_parameter_on_server_side+"')",true)";

More details and full example here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Icarus