JS - document.ready equivelant in plain Javascript function

0 votes
149 views
added Dec 6, 2020 in Javascript by lcjr First Warrant Officer (11,530 points)
function docReady(fn) {
  // checking the DOM
  if (document.readyState === "complete" || document.readyState === "interactive") {
      // call on next available tick
      setTimeout(fn, 1);
  } else {
      document.addEventListener("DOMContentLoaded", fn);
  }
} 
docReady(function() {
	// DOM is loaded and ready for manipulation 
	var text = document.getElementById("hmUrl").value;
	text = text.replace(
		/((http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?)/g,
		'<a href="$1">$1</a>'
	);
});

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...