Tag: javascript
Strip HTML from a string in JavaScript
by admin on May.20, 2011, under Web Development
Nice handy way of removing all HTML from a string using on JavaScript:
variable.replace(/<.*?>/g, '');
There are other ways of doing this, but they rely on the presence of a web browser. This should work in most modern JS engines.
How to Print Part of a Page in JavaScript
by Mike on Dec.21, 2010, under General, Web Development
Recently I was working on a project that needed a ‘print’ button. Usually I work with database-driven content, so creatign a ‘prinkt’ page is fairly simple – I just use a blank page template and load in the content. In thsi project though, there was quite an advanced templating system, which turned out to be not quite so advanced as I couldn’t pull the data from the template view into a blank template…
So anyway. I wanted to use JavaScript to pull in page of the page rather than having to recreate a lot of the templating system. Below is what I made, hopefully you’ll find it useful.
function printDiv()
{
var divToPrint=document.getElementById('page-content');
newWin= window.open("");
newWin.document.write(divToPrint.innerHTML);
newWin.print();
newWin.close();
}
Basically you have a div on your page called ‘page-content’ (or whatever you want). You can then call the function on a button press or whatever ().
The script basically creates a new window, puts the contents of the selected div into it, prints that new page, and then close that new page. The user may see a flicker while that happens, unfortunately unavoidable.
Hope it helps.