Category : JavaScript

Javascript simple clock

Create a simple function that get the client time (hours:minutes:seconds) and prints it in the div: <script type=”text/javascript”> function getTime() { var currentDate = new Date(); var hour = currentDate.getHours(); var min = currentDate.getMinutes(); var sec = currentDate.getSeconds(); var currentTime = hour + “:” + min + “:” + sec; document.getElementById(‘clock’).innerHTML = currentTime; } setInterval(“getTime()”,

Read More →

Add a custom right-click function to a web application

Add a custom right-click function to a web application. Also can be used to implemen a custom menu opened on right-click: <script language=”javascript” type=”text/javascript”> document.oncontextmenu = RightMouseDown; document.onmousedown = mouseDown; function mouseDown(e) { if (e.which == 3) {//righClick console.log(“Right-click goes here”); } } function RightMouseDown() { console.log(“Right-click Context goes here”); return false; } </script>

Get duplicate id’s in DOM using jQuery

Get duplicate id’s in DOM using jQuery // Warning Duplicate IDs $(‘[id]’).each(function(){ var ids = $(‘[id=”‘+this.id+'”]’); if(ids.length > 1 && ids[0] == this) console.warn(‘Multiple IDs #’+this.id); });

Create a new rule for jquery.validator plugin

Create a new simple rule for jquery.validator plugin by doing something like this: jQuery.validator.addMethod(“greaterThanZero”, function(value, element) { return this.optional(element) || (parseFloat(value) > 0); }, “* Amount must be greater than zero”); Apply the new rule: $(‘validatorElement’).validate({ rules : { amount : { greaterThanZero : true } } });

Javascript strip tags

A very useful snippet code to strip html tags using Javascript: var strippedString = string.replace(/(]+)>)/ig,””); Read entire article and commentshere

Set and get cookie with Javascript

Javascript function to set cookie: function setCookie(c_name, value, exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? “” : “; expires=” + exdate.toUTCString()); document.cookie=c_name + “=” + c_value; } Javascript function to get cookie: function getCookie(c_name) { var i,x,y,ARRcookies = document.cookie.split(“;”); for (i=0; i < ARRcookies.length; i++) { x = ARRcookies[i].substr(0,ARRcookies[i].indexOf(“=”));

Read More →

Bookmark Script using jQuery

Here is a bookmark script using jQuery: <script language=”javascript” type=”text/javascript”> $(document).ready(function(){ $(“a.jQueryBookmark”).click(function(e){ e.preventDefault(); // this will prevent the anchor tag from going the user off to the link var bookmarkUrl = this.href; var bookmarkTitle = this.title; if (window.sidebar) { // For Mozilla Firefox Bookmark window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,””); } else if( window.external || document.all) { // For

Read More →

Javascript – get Url

Get Url or Url parts using Javascript: window.location.href – get entire url window.location.protocol – get protocol: “http” / “https” window.location.host – get hostname, e.g: “www.g31zone.com” window.location.pathname – get script name, e.g: “example/index.html”

Loading JavaScript Asynchronously

Sample of loading javascript asyncronously: <script id=”myscript” type=”text/javascript”> (function() { var myscript = document.createElement(‘script’); myscript.type = ‘text/javascript’; myscript.src = (‘http://example.org/myscript.js’); var s = document.getElementById(‘myscript’); s.parentNode.insertBefore(myscript, s); })(); </script> Visit this page for entire article.

jQuery – preview button in new tab(window), target blank

jQuery – preview button in new tab(window), target blank <!– DOCTYPE –> <html> <head> <title>jQuery – preview button in new tab(window), target blank</title> <script src=”http://code.jquery.com/jquery-latest.js”></script> </head> <body> <form id=”my-form” method=”POST” action=””> <input type=”submit” name=”submit” id=”submit” value=”OK”> <input type=”submit” name=”preview” id=”preview” value=”Preview”> </form> <script type=”text/javascript”> jQuery(document).ready(function() { jQuery(‘#preview’).click(function(){ jQuery(‘#my-form’).attr(‘target’, ‘_blank’); }); jQuery(‘#submit’).click(function(){ jQuery(‘#my-form’).removeAttr(‘target’); }); }); </script>

Read More →

1 2 3