<div id="bar" />
<script>
if (document.getElementById('foo') == null) {
alert('foo is missing!');
}
//or:
if ( ! document.getElementById('bar')) {
alert('bar is missing!');
}
</script>
Tag Archive for check
Does an element exist?
Ajax getHTTPObject function
/*Usage
* var request = getHTTPObject();
* if(request){
* AJAX CODE HERE
* }
*
* If getHTTPObject returns false, the browser isn't Ajax compatible. The if
* statement checks to see if it exists, then runs the code.
*/
function getHTTPObject() {
var xhr = false;//set to false, so if it fails, do nothing
if(window.XMLHttpRequest) {//detect to see if browser allows this method
var xhr = new XMLHttpRequest();//set var the new request
} else if(window.ActiveXObject) {//detect to see if browser allows this method
try {
var xhr = new ActiveXObject("Msxml2.XMLHTTP");//try this method first
} catch(e) {//if it fails move onto the next
try {
var xhr = new ActiveXObject("Microsoft.XMLHTTP");//try this method next
} catch(e) {//if that also fails return false.
xhr = false;
}
}
}
return xhr;//return the value of xhr
}
Check for e updates
#!/usr/bin/env ruby
BEGIN {$VERBOSE = true}
require 'date'
require 'fileutils'
require 'time'
LOCAL_FILE = 'e_setup.exe'
REMOTE_FILE = 'http://opencompany.org/download/e_setup.exe'
WGET_CMD = '/usr/bin/wget --spider -S'
def download_run()
`curl -R -O #{REMOTE_FILE}`
puts "Setting file permissions..."
`chmod +x #{LOCAL_FILE}`
puts "Running setup."
`cygstart #{LOCAL_FILE}`
end
unless File.exists?(LOCAL_FILE)
download_run()
exit()
end
puts "Checking modification times..."
begin
mod_time = Time.parse(%x{#{WGET_CMD} #{REMOTE_FILE} 2>&1}.split("
").grep(/Last-Modified:/)[0].split(": ")[1].chomp)
if mod_time > File.mtime(LOCAL_FILE)
puts "Remote file is newer. Downloading..."
download_run()
else
puts "Remote file no newer than local file. Nothing to do."
end
rescue
puts "Error during download process: ",$!
exit(false)
end
Browser Checker
/*-----------------------------------------------------------------------------
* Navigator
* Example Source
document.write(
(Navigator.isWinIE()) ? 'IE' + Navigator.isWinIE() + ' (Win)' :
(Navigator.isMacIE()) ? 'IE' + Navigator.isMacIE() + ' (Mac)' :
(Navigator.isGecko()) ? 'Mozilla (' + Navigator.isGecko() + ')' :
(Navigator.isSafari()) ? 'Safari (' + Navigator.isSafari() + ')' :
(Navigator.isKDE()) ? 'KDE (' + Navigator.isKDE() + ')' :
(Navigator.isOpera()) ? 'Opera' + Navigator.isOpera() :
(Navigator.isNN4()) ? 'NN4' :
"I don't know."
);
*-------------------------------------------------------------------------- */
var Navigator = {
_getVersion: function (a, b) {
var t = navigator.userAgent.split(a)[1];
return (t) ? t.split(b)[0] : false;
},
isOpera: function () {
return (
(window.opera) ?
(document.createElementNS) ?
(document.createCDATASection) ?
(document.styleSheets) ? 9 : 8
: 7
: 6
: false
);
},
isSafari: function () {
return (document.createCDATASection && document.createElementNS) ? Navigator._getVersion('AppleWebKit/', '(') : false;
},
isKDE: function () {
return (document.createCDATASection && document.createElementNS) ? Navigator._getVersion('Konqueror/', ';') : false;
},
isGecko: function () {
return (document.createCDATASection && document.createElementNS) ? Navigator._getVersion('Gecko/', ' ') : false;
},
isNN4: function () {
return (document.layers && typeof document.layers == 'object') ? true : false;
},
isWinIE: function () {
return (
/*@cc_on @if (@_win64 || @_win32 || @_win16)
(document.getElementsByTagName) ?
(@_jscript_version > 5.6) ? 7 :
(@_jscript_version == 5.6) ? 6 :
(@_jscript_version == 5.5) ? 5.5 :
5
: 4
@else@*/false/*@end @*/
);
},
isMacIE: function () {
return (
/*@cc_on @if (@_mac && (@_PowerPC || @_mc680x0))
(document.getElementsByTagName) ? 5 : 4
@else@*/false/*@end @*/
);
}
};
select/deselect-all control
<script type="text/javascript">
<!--
function checkAll(wotForm,wotState) {
for (a=0; a<wotForm.elements.length; a++) {
if (wotForm.elements[a].id.indexOf("delete_") == 0) {
wotForm.elements[a].checked = wotState ;
}
}
}
// -->
</script>
<input type="checkbox" onClick="checkAll(this.form,this.checked);" />
Preload and check movie size with setInterval
_global.queryload = function(clip, intervalname) {
clip.bytes_loaded = Math.round(clip.getBytesLoaded());
clip.bytes_total = Math.round(clip.getBytesTotal());
trace(clip.bytes_loaded + " / " + clip.bytes_total);
if ( clip.bytes_loaded == clip.bytes_total ) {
trace("done");
trace(_root[intervalname]);
clearInterval(_root[intervalname]);
}
}
// To check internally, call it this way:
// preloader = setInterval(queryload, 500, this, "preloader");
// To check externally, call it this way:
// container.loadMovie("catnap.jpg");
// preloader = setInterval(queryload, 500, container, "preloader");
ckeckMail
function checkEmail($email)
{
// Create the syntactical validation regular expression
$regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$";
// Presume that the email is invalid
$valid = 0;
// Validate the syntax
if (eregi($regexp, $email))
{
list($username,$domaintld) = split("@",$email);
// Validate the domain
if (getmxrr($domaintld,$mxrecords))
$valid = 1;
} else {
$valid = 0;
}
return $valid;
}
Check to see if a external image exists
Simple call imageExists(‘http://example.com/image.jpg’); true if it exists else false.
function imageExists($url){
$h = get_headers($url);
return ($h[0] == "HTTP/1.1 404 Not Found")?false:true;
}