all, I want to create a html obejct with javascript in a jsp page, but 'alert(GameObject1.loginurl);' will alert 'undefined'.
Do I get some misstakes in the code below? It seems that 'obj.appendChild' has failed.But Why?
var obj;
try {
obj = document.createElement("object");
obj.id = "GameObject1";
obj.name = "JavaGameObject1";
obj.setAttribute("classid", "clsid:72E6F181-D1B0-4C22-B0D7-4A0740EEAEF5");
obj.width = 640;
obj.height = 526;
var loginurl = document.createElement("param");
loginurl.setAttribute("name", "loginurl");
loginurl.setAttribute("value", "xx.xx.xx.xx:8080");
obj.appendChild(loginurl);
document.body.appendChild(obj);
alert(GameObject1.loginurl);
} catch (e) {
alert("Exception:" + e.message);
}
Based on your code, GameObject1
is never defined. I think you are wanting to use obj
instead as that is the HTML object for the ID GameObject1
.
I will note however that even obj.loginurl
will still be undefined as you created the child HTML object param
called loginurl
, not a property to the HTML object obj
. (ie. You would have needed to do obj.loginurl = "xx.xx.xx.xx:8080"
to access it the way you seem to want)
To get the child element param
's value, you would want something like obj.children[0].value
which will return the value you set on your loginurl
object. Alternatively, in your current scope you could just call loginurl.value
.
When accessing child elements via obj.children[#]
, it is best to do checks to see if the element at that position exists so you don't throw exceptions everywhere.