How to get XML string in javascript?

Here is the function:

function getXmlString(xmlDoc) {
try {
return ((new XMLSerializer()).serializeToString(xmlDoc));
}
catch (e) {
try {
// Internet Explorer.
return (xmlDoc.xml);
}
catch (e) {
//Strange Browser ??
return (‘Xmlserializer not supported’);
}
}
}

How to apply xml to xslt in javascript?

Here is the function:

 

function transFormXml(xmlData, xsltData) {
try {
try {
return xmlData.transformNode(xsltData);
}
catch (e) {
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltData);
return getXmlString(xsltProcessor.transformToFragment(xmlData, document));
}
} catch (e)
{ alert(e); }

return null;
}

How to get time zone offset in javascript?

Here is the function:

 

function getTimeZoneOffset() {
var currentDate = new Date();
var date1 = new Date(currentDate.getFullYear(), 0, 1, 0, 0, 0, 0);
var temp = date1.toGMTString();
var date3 = new Date(temp.substring(0, temp.lastIndexOf(” “)));
var hoursDiffStdTime = ((date1 – date3) / (1000 * 60 * 60)) * 60;
return hoursDiffStdTime;
}

And if you want to get the offset with daylight saving then look at the below function:

 

function getTimeZoneOffsetDayLightSaving() {
var currentDate = new Date();
var date2 = new Date(currentDate.getFullYear(), 6, 1, 0, 0, 0, 0);
var temp = date2.toGMTString();
var date4 = new Date(temp.substring(0, temp.lastIndexOf(” “)));
var hoursDiffDaylightTime = ((date2 – date4) / (1000 * 60 * 60)) * 60;

return hoursDiffDaylightTime;
}

How to allow numbers, dot and comma in a text box in Javascript?

Here is the function:

 

function valid(field) {
var re = /\d*\.\,/;
if (!re.test(field.value)) {
alert(“Only numbers, dot and comma are allowed!”);
}

}

How to remove an element from an array using its index?

If you want to remove an element from an array using its index, then you simply use splice() method

If you want to remove an element from an array using its index, then you simply use splice() method.

This method can be used to add and remove the element from the array in javascript.

Have a look at the below function :

function removeFromArrayByIndex(arr, index) {

arr.splice(index, 1);

}

test = new Array();

test[0] = ‘onion’;

test[1] = ‘tomato’;

test[2] = ‘beans’;

test[3] = ‘peas’;

//alert(“Array before removing elements: “+test);

removeByIndex(test, 2);

//alert(“Array after removing elements: “+test);

Check the results by the alerts.