Monday, July 25, 2016

get all html code (clone) and encode the data

some time you need to save some of html data on localStorage. that time we need to encode that data before saving. this code might be help you.

 var entityMap = {
            "&": "&",
            "<": "<",
            ">": ">",
            '"': '"',
            "'": ''',
            "/": '/'
        };

        function escapeHtml(string) {
            return String(string).replace(/[&<>"'\/]/g, function (s) {
                return entityMap[s];
            });
        }

           $clone = $('html').html();
           console.log("Encoded HTML is = "+escapeHtml($clone));

save a custom text file in your computer using javascript

Write the text and click.. that's it... your text will be exported into text file in your computer.

Uses-: copy it into an .html file and open that file in a web browser that runs JavaScrip.


So, Here the code-


<html>
<body>

<table>
<tr><td>Text to Save:</td></tr>
<tr>
        <td colspan="3">
            <textarea cols="80" id="inputTextToSave" rows="25"></textarea>
        </td>
    </tr>
<tr>
        <td>Filename to Save As:</td>
        <td><input id="inputFileNameToSaveAs" /></td>
        <td><button onclick="saveTextAsFile()">Save Text to File</button></td>
    </tr>
<tr>
        <td>Select a File to Load:</td>
        <td><input id="fileToLoad" type="file" /></td>
        <td><button onclick="loadFileAsText()">Load Selected File</button><td>
    </td></td></tr>
</table>
<script type="text/javascript">

function saveTextAsFile()
{
    var textToSave = document.getElementById("inputTextToSave").value;
    var textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
    var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
    var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;

    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    downloadLink.href = textToSaveAsURL;
    downloadLink.onclick = destroyClickedElement;
    downloadLink.style.display = "none";
    document.body.appendChild(downloadLink);

    downloadLink.click();
}

function destroyClickedElement(event)
{
    document.body.removeChild(event.target);
}

function loadFileAsText()
{
    var fileToLoad = document.getElementById("fileToLoad").files[0];

    var fileReader = new FileReader();
    fileReader.onload = function(fileLoadedEvent) 
    {
        var textFromFileLoaded = fileLoadedEvent.target.result;
        document.getElementById("inputTextToSave").value = textFromFileLoaded;
    };
    fileReader.readAsText(fileToLoad, "UTF-8");
}

</script>

</body>
</html>

Sunday, June 5, 2016

How to implement the polymorphism in PHP oop

Sunday, May 29, 2016

Thursday, April 7, 2016

Add Decimal in your amount at desire position

Code

$amount = 560;
$decimalPosition = 2;
$divideBy   =   pow(10, $decimalPosition );
$newNumber  =   number_format( $totalTax / $divideBy , 2, '.','');
echo $newNumber;

OutPut

5.60


Second Way

        $decimal_point = 2;
        $totalTax=1260; //1.260

        $a=substr($totalTax,0,-$decimal_point); //560 ka 5
        $b=substr($totalTax,-$decimal_point,$decimal_point); //560 ka 60
        
        $c = $a.'.'.$b;
        echo $c;
        


Third Way

        /********* in one line ****************************************\
        
        $d = (substr($totalTax,0,-$decimal_point)).'.'.(substr($totalTax,-$decimal_point,$decimal_point));
        echo $d;