ClanKiller.com http://forums.clankiller.com/ |
|
Some simple javascript stuff http://forums.clankiller.com/viewtopic.php?f=24&t=2190 |
Page 1 of 1 |
Author: | Satis [ Thu Feb 01, 2007 8:20 am ] |
Post subject: | Some simple javascript stuff |
ok, this is stuff I have to look up every time I use it, but I use it often enough for that to be a pain. Nothing ground breaking, just some javascript methods I use regularly and don't want to have to google any more. reload window: [php]window.location.reload()[/php] submit form: [php]document.formname.submit()[/php] get value of a select dropdown: [php]var SelectBoxObject = document.getElementById('selectBoxID'); var SelectedValue = SelectBoxObject.options[SelectBoxObject.selectedIndex].value;[/php] *corrected a typo in the select box one* |
Author: | Pig [ Fri Feb 02, 2007 4:25 pm ] |
Post subject: | |
document.domain = 'domain.tld'; That will allow you to use ajax across subdomains, if you set it on each page invovled. (moz only) |
Author: | Satis [ Wed Feb 28, 2007 7:37 am ] |
Post subject: | |
some short functions check if an input is numeric [php] function isNumeric(sText){ var ValidChars = "0123456789."; var IsNumber=true; var Char; for (i = 0; i < sText.length && IsNumber == true; i++){ Char = sText.charAt(i); if (ValidChars.indexOf(Char) == -1){ IsNumber = false; } } return IsNumber; } [/php] My universal XMLHTTPRequest object getter [php] function getHTTPObject() { var xmlhttp; if (window.XMLHttpRequest) { // Mozilla, Safari,... xmlhttp = new XMLHttpRequest(); if (xmlhttp.overrideMimeType) { xmlhttp.overrideMimeType('text/xml'); } } else if (window.ActiveXObject) { // IE var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"]; for (var i = 0; i < versions.length ; i++) { try { xmlhttp = new ActiveXObject(versions[i]); if (xmlhttp) { _ms_XMLHttpRequest_ActiveX = versions[i]; break; } } catch (objException) { } ; } } return xmlhttp; } var http = getHTTPObject(); // Create the HTTP Object [/php] |
Author: | RB [ Wed Feb 28, 2007 9:34 am ] | |||||||||
Post subject: | ||||||||||
That one is telling me that an empty string is numeric. Now, even if there is no easier way to do it, you could try this out: [php]function isNumeric(sText) { var ValidChars = "0123456789."; if(sText==null || sText.length==0) return false; for (i = 0; i < sText.length; i++) { if (ValidChars.indexOf(sText.charAt(i)) == -1) return false; } return true; }[/php] Faster + more compact if not more correct too. You may like to check too if there is '-' or '+' on the start and as well if '.' occurs only once in the string. Currently it accepts strings like '1232.32131.1232'. Scientific form? (1.23e-10) Have you heard for RegExp btw? |
Author: | Satis [ Fri Mar 02, 2007 6:41 am ] |
Post subject: | |
I've played with regex in php and in c#, but not in javascript. I know I prefer c#'s implementation over php's. |
Author: | RB [ Fri Mar 02, 2007 8:13 am ] |
Post subject: | |
Yeah, it is ugly done in PHP, imo. But there is regex in JS too... let me do a small example (with my basic knowledge on it). This one should be able to check whether the given string contains only an integer or not. [php]function isint(s) { var checkint = new RegExp("^[-|+]{0,1}[0-9]+$"); return s.match(checkint); }[/php] |
Author: | Satis [ Tue Apr 10, 2007 7:55 am ] |
Post subject: | |
a better page reload. This won't try to resubmit a form. [php]document.location.href = document.location.href[/php] |
Author: | Satis [ Wed Jun 13, 2007 7:41 am ] |
Post subject: | |
this'll loop through a bunch of radio buttons on a form and return the selected value. [php] <script> function getValue(){ value = 'undefined'; for(i=0; i < document.test.radios.length; i++){ if(document.test.radios[i].checked == true){ value = document.test.radios[i].value; } } document.getElementById('data').innerHTML = value; } </script> <form name='test'> 0<input type='radio' name='radios' value=0> 1<input type='radio' name='radios' value=1> 2<input type='radio' name='radios' value=2> 3<input type='radio' name='radios' value=3> 4<input type='radio' name='radios' value=4> 5<input type='radio' name='radios' value=5> 6<input type='radio' name='radios' value=6> 7<input type='radio' name='radios' value=7> </form> <a href='javascript: getValue();'>Get Value</a><br> <span id='data'></span> [/php] |
Author: | Pig [ Wed Jul 18, 2007 5:07 pm ] | |||||||||
Post subject: | ||||||||||
Javascript variable scope gotcha. Not a "how to", but a "how not to". Javascript scopes local variables to the function, not to the loop.
To make "a" local to the loop, you have to put the loop inside an anonymous function. |
Page 1 of 1 | All times are UTC - 6 hours |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |