Adding javascript programatically via ASP.net

jumpstart0321
jumpstart0321 used Ask the Experts™
on
Ok, I have some javascript that I can run by inputting it into the <head> section of my code. This Javascript automatically fires on page load. The code takes the syntax:
<head>
        <script type="text/javascript">
Javascript code here
</script>
</head>
This works great, however I need to add the code programatically because the code has dynamic variables that need to be added. So, I took on this method to add the Javascript via code:
        Dim header As LiteralControl = New LiteralControl
        header.Text = "JAVASCRIPT CODE"
        Me.Page.Header.Controls.Add(header)
This code visually shows up fine if I check the source code, however it is not firing. I have checked and recked the source code and it generates identical source as the original. Anybody know what the problem is? If anyone wishes to reference my exact code, I will list it below:
<script type="text/javascript">
var AjaxEnginePage;
var ClientInfoPage;
var XMLHTTP;
AjaxEnginePage = "ProcessBadLink.aspx";
ClientInfoPage="BadLink.aspx"; 
 
    var MerchantID = 21432;
    var requestUrl =AjaxEnginePage + "?MerchantID="+ MerchantID;
    CreateXMLHTTP();
    if(XMLHTTP)
    {
        XMLHTTP.open("POST", requestUrl, true);
        XMLHTTP.send(null); 
    }
 
function CreateXMLHTTP()
{
	try
	{
		XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XMLHTTP = null;
		}
	}
	if(!XMLHTTP && typeof XMLHttpRequest != "undefined") 
	{
		XMLHTTP = new XMLHttpRequest();
	}
}
        </script>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
To execute a javascript in code-behind, one way is...

--Raj


ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('" + <your parameters here> + "');", true);

Open in new window

Rajkumar GsSoftware Engineer
Commented:
Another way is as shown here. This is an example

You need to modify the variable JScript with your javascript and give option to append external parameters you want.

Is this you are asking...?

--Raj
string JScript = "<script type='text/javascript'>" + 
"GetRadWindow().BrowserWindow.location.href = GetRadWindow().BrowserWindow.location.href;" +
"GetRadWindow().close();" +
"</script>";
 
// perform the server-side actions here and then...
Page.RegisterStartupScript("closeWindow", JScript);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial