Last active
February 2, 2021 17:26
-
-
Save belfie13/09c1b3c223e6ce88a0df8e875be59d3d to your computer and use it in GitHub Desktop.
JavaScript snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function defaultParameterValues(veryable) { | |
veryable = veryable || 'default'; | |
} | |
// from html.spec.whatwg.org somewhere | |
function prependTextControlValue(control) { | |
// to add text to the start of a text control (input or textarea element), and keep the text selection | |
var oldStart = control.selectionStart; | |
var oldEnd = control.selectionEnd; | |
var oldDirection = control.selectionDirection; | |
var prefix = "http://"; | |
control.value = prefix + control.value; | |
control.setSelectionRange( | |
oldStart + prefix.length, | |
oldEnd + prefix.length, | |
oldDirection | |
); | |
} | |
// from html.spec.whatwg.org somewhere | |
function getTextControlSelection(control) { | |
// To get the selected text | |
var selectionText = control.value.substring(control.selectionStart, control.selectionEnd); | |
} | |
// Insert some HTML tags or smiles or any custom text in a textarea. | |
function insertMetachars(sStartTag, sEndTag) { | |
var bDouble = arguments.length > 1, | |
oMsgInput = document.myForm.myTxtArea, | |
nSelStart = oMsgInput.selectionStart, | |
nSelEnd = oMsgInput.selectionEnd, | |
sOldText = oMsgInput.value; | |
oMsgInput.value = sOldText.substring(0, nSelStart) | |
+ (bDouble ? | |
sStartTag + sOldText.substring(nSelStart, nSelEnd) + sEndTag : | |
sStartTag | |
) | |
+ sOldText.substring(nSelEnd); | |
oMsgInput.setSelectionRange(bDouble || nSelStart === nSelEnd ? | |
nSelStart + sStartTag.length : | |
nSelStart, | |
(bDouble ? nSelEnd : nSelStart) + | |
sStartTag.length | |
); | |
oMsgInput.focus(); | |
} | |
function loops(iterable) { | |
// define vars before loop to avoid reprocessing. | |
var length = iterable.length; | |
for (var i = 0; i < length; i++) { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
</head> | |
<body> | |
it's generally better to avoid using noscript, and to instead design the script to change the page from being a scriptless page to a scripted page on the fly, as in the next example: | |
<form action="calcSquare.php"> | |
<p> | |
<label for=x>Number</label>: | |
<input id="x" name="x" type="number"> | |
</p> | |
<input id="submit" type=submit value="Calculate Square"> | |
<script> | |
var x = document.getElementById('x'); | |
var output = document.createElement('p'); | |
output.textContent = 'Type a number; it will be squared right then!'; | |
x.form.appendChild(output); | |
x.form.onsubmit = function () { return false; } | |
x.oninput = function () { | |
var v = x.valueAsNumber; | |
output.textContent = v + ' squared is ' + v * v; | |
}; | |
var submit = document.getElementById('submit'); | |
submit.parentNode.removeChild(submit); | |
</script> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment