I have the jQuery function below (Show_popupWindows
) :
$(function () {
var popupWindows_Done = $('input[type="hidden"]#hfpopupWindows_Done').val();
if (popupWindows_Done == "false") {
Show_popupWindows();
}
else {
}
}); //End Of $(function ()
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function Show_popupWindows() {
var x = screen.availWidth;
//alert(x);
$('div#OffDiv').css({ 'width': x });
//alert($('div#OffDivAndLoadingContainer').css('width'));
var y = screen.availHeight;
//alert(y);
$('div#OffDiv').css({ 'height': y });
//alert($('div#OffDivAndLoadingContainer').css('height'));
//alert('On');
$('div#OffDiv').css({ 'display': 'block' });
$('div#TimerContainer').css({ 'display': 'block' });
var w1 = window.open('http://www.blablabla.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);
var w2 = window.open('http://www.blablabla.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);
window.focus();
var sec = $('#TimerContainer span').text()
var timer = setInterval(function () {
$('#TimerContainer span').text(--sec);
if (sec == 0) {
clearInterval(timer);
$('div#OffDiv').css({ 'display': 'none' });
$('div#TimerContainer').css({ 'display': 'none' });
}
}, 1000);
var watchClose = setInterval(function () {
try {
if (w1.closed || w2.closed) {
clearTimeout(watchClose);
clearInterval(timer);
DoNotClosePoPupWindowDuringTimer();
}
}
catch (e) {
}
}, 200);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
how can I put this function into my code behind, so nobody can change this function by editing aspx file.
I tried the method below in code behind :
private static StringBuilder Create_ShowpopupWindows_Function()
{
StringBuilder sb = new StringBuilder();
sb.Append("var x = screen.availWidth;");
sb.Append("//alert(x);");
sb.Append("$('div#OffDiv').css({ 'width': x });");
sb.Append("//alert($('div#OffDivAndLoadingContainer').css('width'));");
sb.Append("var y = screen.availHeight;");
sb.Append("//alert(y);");
sb.Append("$('div#OffDiv').css({ 'height': y });");
sb.Append("//alert($('div#OffDivAndLoadingContainer').css('height'));");
sb.Append("//alert('On');");
sb.Append("$('div#OffDiv').css({ 'display': 'block' });");
sb.Append("$('div#TimerContainer').css({ 'display': 'block' });");
sb.Append("var w1 = window.open('http://www.almasafzar.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);");
sb.Append("var w2 = window.open('http://www.soscharge.com', '_blank', 'channelmode=no,directories=yes,location=no,resizable=yes,titlebar=yes,menubar=no,toolbar=no,scrollbars=yes,status=yes', false);");
sb.Append("window.focus();");
sb.Append("var sec = $('#TimerContainer span').text()");
sb.Append("var timer = setInterval(function () {");
sb.Append("$('#TimerContainer span').text(--sec);");
sb.Append("if (sec == 0) {");
sb.Append("clearInterval(timer);");
sb.Append("$('div#OffDiv').css({ 'display': 'none' });");
sb.Append("$('div#TimerContainer').css({ 'display': 'none' });");
sb.Append("}");
sb.Append("}, 1000);");
sb.Append("var watchClose = setInterval(function () {");
sb.Append("try {");
sb.Append("if (w1.closed || w2.closed) {");
sb.Append("clearTimeout(watchClose);");
sb.Append("clearInterval(timer);");
sb.Append("DoNotClosePoPupWindowDuringTimer();");
sb.Append("}");
sb.Append("}");
sb.Append("catch (e) {");
sb.Append("}");
sb.Append("}, 200);");
return sb;
}
but it does not work when I call it in page_load :
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = Create_ShowpopupWindows_Function();
ScriptManager.RegisterStartupScript(this, this.GetType(), "Show_popupWindows", sb.ToString(), true);
}
Instead of creating the script using StringBuilder
in code behind to avoid the customer (taking that from your comments on your question) to manipulate the script you can embed the script file in the assembly file.
[assembly: System.Web.UI.WebResource("SomeFolder.myJavascript.js", "text/js")]
Include the script on the page:
string scriptLocation =
Page.ClientScript.GetWebResourceUrl(this.GetType(), "SomeFolder.myJavascript.js");
Page.ClientScript.RegisterClientScriptInclude("SomeFolder.myJavascript.js", scriptLocation);