newWindow(mainHTML, headHTML, footHTML) [Javascript]

Try­ing out a new Visual Code Edi­tor plu­gin for Word­Press. Below is a Javascript func­tion that I wrote (lever­ag­ing jQuery) for pop­ping a modal win­dow. So far, it’s made its way into two websites.

[javascript]
func­tion newWindow(mainHTML, head­HTML, footHTML){
if (!main­HTML) { var main­HTML = “”; }
if (!head­HTML) { var head­HTML = “”; }
if (!footHTML) { var footHTML = “”; }

if (!$(“#popupcontent”).length){
$popout = $(“<div id=\“popup\”></div>”).addClass(“popup”);
} else {
$popout = $(“#popup”).empty();
if (!$popout.hasClass(“popup”)) { $popout.addClass(“popup”); }
}

$popup = $(“<div id=\“popupcontent\”></div>”);
$(“body”).append($popout.append($popup));

$close = $(“<a>Close</a>”).addClass(“close”).click(function(){ $(“#popup”).remove();});
$pop­up­Head = $(“<div id=\“popuphead\”></div>”).html(headHTML).prepend($close);
$pop­up­Foot = $(“<div id=\“popupfoot\”></div>”).html(footHTML);
$popup.html(mainHTML);
$popout.prepend($popupHead).append($popupFoot);

$popout.css({
position:“absolute”,
top:($(window).height()-$popout.outerHeight())/2,
left:($(window).width()-$popout.outerWidth())/2
});
}
[/javascript]

3 thoughts on “newWindow(mainHTML, headHTML, footHTML) [Javascript]

  1. Enti­ties for intended? For exam­ple, on lines 16, 17, and 18?

    Seems like you would want lit­er­als there, based on the lit­tle expe­ri­ence I have with jQuery…

    • Actu­ally, that’s a bug in the Word­Press + plu­gin rela­tion­ship. In my actual code, you can rest assured that I have no great col­lec­tion of < and >. *sigh* I guess I’ll have to keep try­ing plu­g­ins before I can eas­ily post source code samples.