Tutorial Check if Number is Even/Odd
by Unknown in javascript , Tutorial 0
function isEven(value) {
if (value%2 == 0)
return true;
else
return false;
}
by Unknown in javascript , Tutorial 0
function isEven(value) {
if (value%2 == 0)
return true;
else
return false;
}
by Unknown in javascript , Tutorial 0
When using scripts that are shared between different areas of a site, there may be cases where a function is called that doesn't exist. It makes sense on one page (the dependency is there) but not another. The difference is too slight to warrant forking the file into different versions. Instead, you can just check if the function exists before calling it to avoid the error:
if (typeof yourFunctionName == 'function') {
yourFunctionName();
}
by Unknown in javascript , Tutorial 0
function checkFont(strFamily) {
var objDiv = document.createElement('div');
objDiv.style.fontFamily = strFamily;
objDiv.appendChild(document.createTextNode('FONT TEST'));
if (window.getComputedStyle) {
return window.getComputedStyle(objDiv, null).getPropertyValue('font-family') === strFamily;
}
return objDiv.currentStyle.fontFamily === strFamily;
}
Usage
var iconFontHonored = checkFont('icomoon');
by Unknown in javascript , Tutorial 0
function randRange(data) {
var newTime = data[Math.floor(data.length * Math.random())];
return newTime;
}
function toggleSomething() {
var timeArray = new Array(200, 300, 150, 250, 2000, 3000, 1000, 1500);
// do stuff, happens to use jQuery here (nothing else does)
$("#box").toggleClass("visible");
clearInterval(timer);
timer = setInterval(toggleSomething, randRange(timeArray));
}
var timer = setInterval(toggleSomething, 1000);
// 1000 = Initial timer when the page is first loaded
by Unknown in javascript , Tutorial 0
if (top.location!= self.location) {
top.location = self.location.href;
}
That will generally work, but there is a small chance of failure in the case that window is overridden. Here's a couple of clever alternatives from Nathan Smith:
<script>
// Break out of an iframe, if someone shoves your site
// into one of those silly top-bar URL shortener things.
//
// Passing `this` and re-aliasing as `window` ensures
// that the window object hasn't been overwritten.
//
// Example:
// var window = 'haha, punked!';
//
// Note: Probably unnecessary, but just for kicks.
(function(window) {
if (window.location !== window.top.location) {
window.top.location = window.location;
}
})(this);
</script>
<script>
// A more cryptic one-liner, to awe & impress.
//
// No need to protect `window` since `this` is
// immutable, and at the topmost level means
// `window` anyways. Here, we compare locations
// on the left side of the "&&" and execute the
// code in parenthesis if that condition is
// true (top location isn't iframe location).
//
// Otherwise, nothing happens. It's basically an
// if statement without wrapping curly brackets.
//
// Weird, I know. But pretty cool, right? :)
this.top.location !== this.location && (this.top.location = this.location);
</script>
by Unknown in javascript , Tutorial 0
<script type="text/javascript">
alert('ALERT!')
</script>
by Unknown in javascript , Tutorial 0
<textarea rows="10" cols="50" readonly="readonly">
example text
</textarea>