more hidden iframe love

2007-11-14 @ 12:26#

new rule - all my XHTML templates will include a hidden <iframe> at the bottom. like this:

<iframe name="hidden-iframe" id="hidden-iframe" style="display:none;" marginheight="0" marginwidth="0" scrolling="no" frameborder="0"></iframe>

i can now use standard HTML POST forms w/ a target='hidden-iframe' to allow posting data to the server without navigating away from the current page.

i also use a template in my server scripts to responding to these 'iframe-posts' by squirting some javascript into the target iframe that can pop-up a dialog with results. like this:

// emit a small html page that pops a dialog
public void EmitClientAlert(string code, string message)
{
    EmitClientAlert(code, message, true);
}
public void EmitClientAlert(string code, string message, bool showCode)
{
    string output = "<html>";
    output += "<head>";
    output += "<script type='text/javascript'>\n";
    output += "if('{@code}'=='200')\n";
    output += "alert(\"{@message}\");\n";
    output += "else\n";

    if(showCode==true)
        output += "alert(\"ERROR: {@code}\\n\\n{@message}\");\n";   
    else
        output += "alert(\"ERROR: \\n{@message}\");\n";   

    output += "<" + "/" + "script>\n";
    output += "</head><body></body></html>";

    output = output.Replace("{@code}", code);
    output = output.Replace("{@message}", message.Replace(@"\",@"\\"));

    // complete the response
    HttpContext.Current.Response.ContentType = "text/html";
    HttpContext.Current.Response.Write(output);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Close();
}

a *very handy* pattern!

code