日积月累
-
getElementsByTagName返回的是一个数组吗?
2008-10-07
getElementsByTagName返回的是一个数组吗?用起来像是一个数组,用length属性表示元素个数,用数字下表访问集合内的元素,但请看看下面的这个例子
页面元素如下:
<div>1</div>
<div>2</div>
<div>3</div>执行js脚本:
var divs = document.getElementsByTagName('DIV');
alert(divs.length);
var div4 = document.createElement('DIV');
div4.innerHTML = '4';
document.body.appendChild(div4);
alert(divs.length);第一个alert输出结果是3,如果getElementsByTagName返回的是一个数组,第二次alert输出也应该是3,但实际输出是4,说明getElementsByTagName返回的是一个活动的列表,会实时反映出DOM对象的更新情况。
-
Character URL Encoded Table
2008-03-07
参考文章:INTRODUCTION TO URL ENCODINGCharacter URL Encoded ; %3B ? %3F / %2F : %3A # %23 & %24 = %3D + %2B $ %26 , %2C <space> %20 or + % %25 < %3C > %3E ~ %7E % %25 -
一个避免无意中鼠标滚轮滚动,而导致当前选择框改变的脚本
2008-02-01
window.attachEvent("onload", function() {
selects = document.getElementsByTagName('SELECT');
for (var i = 0; i < selects.length; i++ ) {
selects[i].old_onchange = selects[i].onchange;
selects[i].onchange = function() {
this.old_onchange();
window.focus();
}
}
}); -
取HTML元素自定义属性IE和FF的一点差异
2008-02-01
像这样的元素:<input id="c" type="text" format="0.##" />
取出format属性,在IE下面可以使用:document.getElementById('cust_id').format,但是在FF下面只能使用document.getElementById('cust_id').getAttribute('format'),当然这个方法在IE下面也是可以使用的。
-
Use Javascript in Java 6
2007-11-05
Here is a simple example from dev2dev website:
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class SimpleScript {
public static void main(String[] args) throws ScriptException,
FileNotFoundException, NoSuchMethodException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
// output value of vairable
engine.put("name", "hello, adam");
engine.eval("println(name)");
// evaluate a statement
engine.put("age", 15);
engine.eval("if (age > 18) {" + "println('you can vote!')" + "} else {"
+ "println('you should be in school!')" + "}");
// read script from file
engine.put("age", 20);
engine.eval(new FileReader("/home/adam/workspace/script/bin/test.js"));
// return a value from script
// if you are thirty years old and gendar is true, result is "Me",
// otherwise
// return value "Not me"
engine.put("age", 31);
engine.put("gendar", Boolean.TRUE);
Object result = engine.eval(new FileReader(
"/home/adam/workspace/script/bin/me.js"));
System.out.println(result);
// exchange information in Java and Javascript via bindings
Bindings bindings = engine.createBindings();
bindings.put("color", "null");
engine.eval("color = 'red';", bindings);
String color = (String) bindings.get("color");
System.out.println(color);
// call a java class's method in js script
engine.eval("name2 = name.toUpperCase();" + "println(name2);");
// create a java class instance
engine.eval("importPackage(java.util);" + "today = new Date();"
+ "println('Today is: '+ today);");
// compiled script
Compilable compilable = (Compilable) engine;
CompiledScript script = compilable.compile("println('hello, world!')");
script.eval();
// java call method in js script
engine.eval("function increment(i) { return i + 1; };");
Invocable invocable = (Invocable) engine;
Object i = invocable.invokeFunction("increment", new Object[] { 100 });
System.out.println(i);
}
}Content of file me.js :
if (age > 30 && gendar == true) {
result = 'Me';
} else {
result = 'Not me';
}Content of file test.js:
if (age > 18) {
println('you can vote!');
} else {
println('you should be in school!');
}


