javascript-snippets
JavaScript Bereich
JavaScript Bereich
<script type="text/javascript">
// <![CDATA[
"use strict";
function calc( form ) {
"use strict";
alert ("here in f") ;
} // function calc
// ]]>
</script>
externes Javascript
<script type="text/javascript" src="bsp.js"></script>
Function
Function
function f() {
"use strict";
return;
}
console.log
console.log
console.log( "hier beim Punkt1" );
onload-Event
onload-Event
window.onload = function() {
" use strict";
} // onload
Ajax
getElementById
let element = document.getElementById("id");
innerHTML
element.innerHTML = '<h2>Hier ist eine Überschrift</h2>';
element.style.display = 'block'; // Einschalten
element.style.display = 'none'; // Ausschalten
AJAX-Bereich
<script type="text/javascript">
// <![CDATA[
"use strict";
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = readajax;
function startAjax(form) {
"use strict";
let param = "Ajaxb.jsp?Wert="123"&Name=strName;
alert(param);
xmlhttp.open("GET", param);
xmlhttp.send();
} // cstartAjax
function readajax() {
"use strict";
// alert(xmlhttp.readyState);
let elementAjax = document.getElementById("ajax");
if (xmlhttp.readyState == 4) {
//alert(xmlhttp.responseText);
//elementAjax.innerHTML= xmlhttp.responseText;
// let obj = JSON.parse(xmlhttp.responseText);
elementAjax.innerHTML = "<h2> Ajax Text </h2> ";
} // if
} //readajax
// ]]>
</script>
Validation
let valid = form.checkValidity();
if (valid) {
// action
}
else {
alert('Bitte gültige Werte eintragen');
} // validation
Access to form-elements
access to checkbox
if (form.myChk.checked ) {
}
access to radiobutton
let select=""
for (var i=0; i<form.rb.length; i++) {
if (form.rb[i].checked) {
select = form.rb[i].value;
} // if
} // for
access to ComboBox
index = form.myCombobox.selectedIndex; // form.myCombobox.options[i].value
access to ComboBox (Multiple)
for (i = 0; i < form.wochentag.length; i++) {
if (form.wochentag.options[i].selected) {
alert("Gewählter Index: " + (i) + "
" + form.wochentag.options[i].value);
} // if
} // for
Parse, IsNaN, Array, Regex
parseInt
let n = parseInt(sn);
parseFloat
let n = parseFloat(sn,10);
IsNaN
if ( isNaN(sn) ) {
}
else {
} // if isNaN
String methods
let n = str_input.length;
.indexOf("abc"); // return the position of abc, -1=not found, no regular expression, faster
item = item.replace("abc", "xyz");
// extract from the string, start, end (0->n-1)
// startindex<y0, then it start from the end
.slice(1,5);
var items = mystring.split(','); // split the string between the comma
.substr(1,5); // start<= chars <= end; (0->n-1)
.substring(1,5); // start<= chars < end; (0->n-1)
.toLowerCase();
.toUpperCase();
.trim(); // remove the spaces at the start and end
Array
let n = str_input.length;
Regex
let regex = new RegExp('^[A-ZÄÖÜ]{1,1}[a-zäüöß]{1,29}$');
if (!regex.test(form.firstname.value)) {
alert('Fehlerhafter Vorname');
return;
}