Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

Wednesday, March 6, 2013

Javascript to convert HTML entity code to Original Character and viceversa

 
The following javascript functions will be useful when working with special characters in HTML.
 
First function converts html entities to literal characters, second function does the reverse

// This Fucntion will decode an entity to its original character
// eg & will become '&' and '"' will become "
function entityToText(entityText) {
      var d = document.createElement("div");
      d.innerHTML = entityText;
      return d.firstChild.nodeValue;
}
 
// This Fucntion will encode a special character to its entity
// eg ‘&’ will become '&' and " will become '"'
function textToEntity(specialText) {
     var d = document.createElement("div");
     d.appendChild(document.createTextNode(specialText));
     return d.innerHTML;
}
 
 
alert(entityToText('&lt;&gt;&amp;&quot;')); // returns <>&"
alert(textToEntity('<&>')); // returns &lt;&amp;&gt;
 
 
.

Monday, July 13, 2009

Hide an html element while printing the web page.

We may encounter certain scenarios where we may have to hide some part of our web page from printing.
Eg: A printer icon shown in a print preview page. Or a price column in the shopping cart etc.

we can use css for hiding the html elements while printing.

1. Add the following css class to your stylesheet.

@media print {
.printHide {
display:none;
}
}


2. Add the style class, printHide for the elements which you want to hide.

The following eg code will hide the link "print" while printing the page.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<style>
@media print {
.printHide {
display:none;
}
}
</style>
</HEAD>

<BODY>
<a href="javascript:void(0);" onclick="window.print();"
class="printHide">Print This Page</a>

<p>
Text Text Text Text Text
Text Text Text Text Text
</p>

<p>
Text Text Text Text Text
Text Text Text Text Text
</p>

</BODY>
</HTML>