Js
Since the release of the [JavaScript] custom MessageBox, many netizens have written to ask how to invoke the Showinfo method on the server side and take a compromise approach to the weekend.
First of all, we should be clear first, why we use Page.write to the custom JS method output to the page why IE is not recognized, there will be "xxx undefined" error. The reason is simple, because the script we output with Page.write is at the top of the page. IE read the JavaScript function, it began to execute, but at this time we link JS file is not read by IE, so IE can not identify our definition in the JS file inside the method. Why would write alert do that? Because alert is an IE-embedded scripting function, IE recognizes it regardless of the page.
Find out where the problem is, and it's all settled:
1. Embed our custom approach into IE. ---> looks a little whimsical, huh?
2, wait for the page load completed and then trigger the event. ---> triggers the event, yes.
How do I know if the page is loaded and completed?
1, through the state of document
2, through the event trigger (Windows.onload)
The first one seems to be more uninsured, sometimes it is all finished, it is still displayed in the transmission of data (FF this situation most obvious), so still with the incident more insurance.
Define a simple method, mount it inside the windows.onload, and do a logo when executed
var loadcomplete = false;
function loadcompleted ()
{
Loadcomplete= true;
}
Window.attachevent ("onload", loadcompleted);
Oh, so we just judge LoadComplete to get the page is loaded.
var mimgdir = "";
var mcaption = "caption";
var mmsg = "message";
var mokclick= null;
function ShowMessage (Imgdir,caption,msg,okclick)
{
if (LoadComplete)
{
Kmessagebox.showinfo (Mimgdir,mcaption,mmsg,mokclick);
}
}
Such a child in the case of LoadComplete not false, we will not go to execute the Kmessagebox.showinfo () method, only then will not appear JS error prompts.
Just like this, not yet, because the output of such a script, ie only in the output of the page when the execution, but at this time loadcomplete=false, so we need to periodically check whether the page is loaded. When it comes to timing, it's only a settimeout & SetInterval. We need to keep testing here, so we use the SetInterval method. The final code is as follows:
var loadcomplete = false;
var mimgdir = "";
var mcaption = "caption";
var mmsg = "message";
var mokclick= null;
var Timerid;
function ShowMessage (Imgdir,caption,msg,okclick)
{
if (LoadComplete)
{
Kmessagebox.showinfo (Mimgdir,mcaption,mmsg,mokclick);
Uninstall this event window.detachevent ("onload", function () {loadcompleted;}); Stop timed Trigger
Window.clearinterval (Timerid);
}
}
function loadcompleted () {loadcomplete=true;}
Window.attachevent ("onload", loadcompleted);
Set up timing detection mechanism
Timerid = Window.setinterval (showmessage,1);
Of course, the above code only compatible with IE, because the use of attachevent and detachevent, as well as other browsers to allow him to see [JavaScript] custom title display way in the article How to:
if (!document.attachevent)//not IE
{
Document.attachevent = function () {Document.addeventlistener (Arguments[0].substr (2), arguments[1],arguments[2])}
}
if (!window.attachevent)//not IE
{
Window.attachevent = function () {Window.addeventlistener (Arguments[0].substr (2), arguments[1],arguments[2])}
}
On the server side, just StringBuilder out the script and write it out. Above also just provides a kind of thinking, of course there will be other methods, such as I do not apply to the timing test, I directly mounted to the windows.onload inside, so that the page automatic monitoring, automatic implementation, also may be:), is called All Roads pass Romama ~~~~~
The above ideas from Yui, and Yui realized a more beautiful custom MessageBox, interested friends can study together.
Http://www.cnblogs.com/walkingboy/archive/2006/08/28/autorun_customerfunction.html