JavaScript: Create a new window with dynamic contents

by CarcaBot on July 24, 2009 · 0 comments

This little function will enable you to create a new window and add contents dynamically, you don't need to have an HTML page created to point it to. This is a great tool for pop-up closeups of images and such.

function newWindow(bodyContent, title)
{
        var newWindow, newContent
       
        newContent = '<html>'
                + '<head>'
                + '<title>' + title + '</title>'
                + '<link rel="stylesheet" href="styles.css" type="text/css" />'
                + '</head>'
                + '<body>'
                +  bodyContent
                + '</body>'
                + '</html>';

        newWindow = window.open("","","scrollbars=yes, height=450,width=550");
        newWindow.focus();
        newWindow.document.write(newContent);
        newWindow.document.close();
}

You could call it like this:

<img src="something_tn.jpg" onclick="newWindow('<img src=something.jpg>','Close up')" />

Leave a Comment

Previous post:

Next post: