Javascript String getWidth(fontSize)

Description

Javascript String getWidth(fontSize)




String.prototype.getWidth = function(fontSize)
{
    var span = document.getElementById("__getwidth");
    if (span == null) {
        span = document.createElement("span");
        span.id = "__getwidth";
        document.body.appendChild(span);
        span.style.visibility = "hidden";
        span.style.whiteSpace = "nowrap";
        span.style.height = 20 + 'px';
    }/*w ww. j av  a  2s.  c o  m*/
    span.innerText = this.replace("\n","");
    span.style.fontSize = fontSize + "px";

    return span.offsetWidth;
}



PreviousNext

Related