It is currently Thu Mar 28, 2024 7:34 am



Reply to topic  [ 9 posts ] 
Some simple javascript stuff 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 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*

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Last edited by Satis on Mon Aug 06, 2007 7:55 am, edited 1 time in total.



Thu Feb 01, 2007 8:20 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
document.domain = 'domain.tld';

That will allow you to use ajax across subdomains, if you set it on each page invovled. (moz only)


Fri Feb 02, 2007 4:25 pm
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
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]

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Wed Feb 28, 2007 7:37 am
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Satis wrote:
check if an input is numeric

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?

_________________
++


Wed Feb 28, 2007 9:34 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
I've played with regex in php and in c#, but not in javascript. I know I prefer c#'s implementation over php's.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Fri Mar 02, 2007 6:41 am
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
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]

_________________
++


Fri Mar 02, 2007 8:13 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
a better page reload. This won't try to resubmit a form.

[php]document.location.href = document.location.href[/php]

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Tue Apr 10, 2007 7:55 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
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]

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Wed Jun 13, 2007 7:41 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Javascript variable scope gotcha.

Not a "how to", but a "how not to". Javascript scopes local variables to the function, not to the loop.

Code:
<script>
function foo(){
    var a = 1;
    for( var a = 0; a < 10; a++ ){
       //...
    }
    alert(a);   //a is 10
}
foo();
</script>


To make "a" local to the loop, you have to put the loop inside an anonymous function.


Wed Jul 18, 2007 5:07 pm
Profile YIM WWW
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.