DEV Community

S M Ashraful Azom
S M Ashraful Azom

Posted on

How to avoid document.write from this code?

    var banglaDigit = {'0': '০','1': '১','2': '২','3': '৩','4': '৪','5': '৫','6': '৬','7': '৭','8': '৮','9': '৯'};
    var dayName = new Array("রবিবার", "সোমবার", "মঙ্গলবার", "বুধবার", "বৃহস্পতিবার", "শুক্রবার", "শনিবার")

    var monName = new Array("জানুয়ারী", "ফেব্রুয়ারী", "মার্চ", "এপ্রিল", "মে", "জুন", "জুলাই", "আগষ্ট",
        "সেপ্টেম্বর", "অক্টোবর", "নভেম্বর", "ডিসেম্বর")

    var now = new Date;

    document.write("আজ " + dayName[now.getDay()] + ", " + now.getDate().toString().replace(/[0123456789]/g, function(s) {
                return banglaDigit[s];
            }) + " " + monName[now.getMonth()] + " " + now
        .getFullYear().toString().replace(/[0123456789]/g, function(s) {
                return banglaDigit[s];
            }) + "ইং ")

    date = new Date();
    hours = date.getHours();
    mins = date.getMinutes();
    secs = date.getSeconds();

    shift='';
    if (hours == 0) {
        shift = " AM";
        hours = 12
    } else if (hours <= 3) {
        shift = " AM"
    } else if (hours == 12) {
        shift = " PM";
        hours = 12
    } else if (hours >= 13) {
        shift = " PM";
        hours -= 12
    }

    if (mins <= 9) {
        mins = "0" + mins
    }

    document.write("* সময়ঃ " + hours.toString().replace(/[0123456789]/g, function(s) {
                return banglaDigit[s];
            }) + " টা :" + mins.toString().replace(/[0123456789]/g, function(s) {
                return banglaDigit[s];
            }) + " মিনিট *");
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
vonheikemen profile image
Heiker •

You can use an html element that is already in your page and set the textContent property.

For example:

Assuming the page has this html.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <div id="display-text"></div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

This is what I would do to show some text:

const display = document.getElementById('display-text');

display.textContent = 'My content goes here';
Enter fullscreen mode Exit fullscreen mode

I recommend reading this nice article: Safely inserting external content into a page.

Collapse
 
ashrafulazom profile image
S M Ashraful Azom •

Thank You