Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-12-10 04:40:07
Point:500 Replies:20 POST_ID:828545USER_ID:11059
Topic:
Perl Programming Language;;JavaScript
I am trying to alias document.getElementId("container").getElementsByTagName("table")[0] . So I created two function one, gid and the other ,gtn for getElementById
and ByTagName respectively. If I run code with alert(gid("container").innerHTML));
or alert(gtn("table")[0].innerHTML); that is no issue. but when combine
two function, gid or gtn to use that will create error such as using
alert(gid("container").gtn("table")[0].innerHTML); that reported invalid error for
document.getElementById("container").document.getElementsByTagName("table")[0]when combining two function gtd and gtn because correct sytnax should
be document.getElementById("container").getElementsByTagName("table")[0]
Anyone has code for combining function for gid() and gtn(0 code for achieving what
I need.
I know Jquery can be easily doing for that but I want do it for my own code or without
Jqurey code help that will be good for us
study and that will help know how Jquery can make it easier or its code behind .
If you have code to know how Jquery do that that willl be welcome to send it to me.
Please don't send me the Jquery library path and word about Jquery already do that for you besides you know how
THis article link might help to know how Jquery can make that easier
http://stackoverflow.com/questions/954417/make-alias-to-document-getelementbyid-in-javascript
Please advise
Duncan
and ByTagName respectively. If I run code with alert(gid("container").innerHTML));
or alert(gtn("table")[0].innerHTML); that is no issue. but when combine
two function, gid or gtn to use that will create error such as using
alert(gid("container").gtn("table")[0].innerHTML); that reported invalid error for
document.getElementById("container").document.getElementsByTagName("table")[0]when combining two function gtd and gtn because correct sytnax should
be document.getElementById("container").getElementsByTagName("table")[0]
Anyone has code for combining function for gid() and gtn(0 code for achieving what
I need.
I know Jquery can be easily doing for that but I want do it for my own code or without
Jqurey code help that will be good for us
study and that will help know how Jquery can make it easier or its code behind .
If you have code to know how Jquery do that that willl be welcome to send it to me.
Please don't send me the Jquery library path and word about Jquery already do that for you besides you know how
THis article link might help to know how Jquery can make that easier
http://stackoverflow.com/questions/954417/make-alias-to-document-getelementbyid-in-javascript
Please advise
Duncan
<html><head><script type="text/javascript">alert(gid("container").gtn("table")[0].innerHTML);function gid(id){var mdoc = new Object;mdoc=document.getElementById(id);return mdoc;}function gtn(tn){var mdoc = new Object;mdoc=document.getElementsByTagName(tn);return mdoc;}</script></head><body><div id="container"><table><tr><td>1232</td></tr></table></div></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:
Author: duncanb7 replied at 2024-12-14 03:54:45
typing mistake because I am tyring in EE instead of copying the real code to you , s
o not gtd , it should be gid and <div id="container">
<table><tbody><tr><td><table><tbody><tr><td>1232</td></tr></tbody></table></td></tr></tbody></table>
</div>
After re-test
alert(gid("container").gtn("table")[0].gtn("table")[0].innerHTML);
it is no problem at all in FF but still not working in IE7.
o not gtd , it should be gid and <div id="container">
<table><tbody><tr><td><table><tbody><tr><td>1232</td></tr></tbody></table></td></tr></tbody></table>
</div>
After re-test
alert(gid("container").gtn("table")[0].gtn("table")[0].innerHTML);
it is no problem at all in FF but still not working in IE7.
<html><head> <script type="text/javascript"> var addDOMMethod = (function () { var ie7 = /*@cc_on!@*/false && typeof HTMLElement == "undefined" && typeof Node == "undefined"; if (ie7) { var methods = {}; return function (that, name, func) { methods[name] = func; document[name] = func; that[name] = (function (func, methods) { return function (arg) { var ret = func.call(document, arg); for (var i in methods) { ret[i] = methods[i]; } return ret; } })(func, methods); }; } else { return function (that, name, func) { if (typeof HTMLElement != "undefined") { HTMLElement.prototype[name] = func; } else if (typeof Node != "undefined") { Node.prototype[name] = func; } if (typeof HTMLDocument != "undefined") { HTMLDocument.prototype[name] = func; } else { document[name] = func; } that[name] = (function (func) { return function (arg) { return func.call(document, arg); }; })(func); }; } })(); addDOMMethod(this, "gid", function(id) { return this.getElementById(id); }); addDOMMethod(this, "gtn", function(tn) { return this.getElementsByTagName(tn); }); var init = function () { var cont = gid("container"); // equal to document.getElementById("container"); var tab = cont.gtn("table"); // equal to document.getElementById("container").getElementsByTagName("table"); alert(tab[0].innerHTML); alert(gid("container").gtn("table")[0].gtn("table")[0].innerHTML); } </script></head><body onload="init();"><div id="container" onload="init();"> <table><tbody><tr><td><table><tbody><tr><td>1267</td></tr></tbody></table></td></tr></tbody></table></div></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:
Accepted Solution
Author: duncanb7 replied at 2024-12-13 03:29:18
No food, please food provider
Duncan
Duncan
Author: duncanb7 replied at 2024-12-12 05:20:17
line14- for (var i in methods) {
line-49- var init = function () {
Both lines reports error
Please advise
Duncan
line-49- var init = function () {
Both lines reports error
Please advise
Duncan
Author: duncanb7 replied at 2024-12-12 05:17:08
typing mistake because I am tyring in EE instead of copying the real code to you , s
o not gtd , it should be gid and <div id="container">
<table><tbody><tr><td><table><tbody><tr><td>1232</td></tr></tbody></table></td></tr></tbody></table>
</div>
After re-test
alert(gid("container").gtn("table")[0].gtn("table")[0].innerHTML);
it is no problem at all in FF but still not working in IE7.
o not gtd , it should be gid and <div id="container">
<table><tbody><tr><td><table><tbody><tr><td>1232</td></tr></tbody></table></td></tr></tbody></table>
</div>
After re-test
alert(gid("container").gtn("table")[0].gtn("table")[0].innerHTML);
it is no problem at all in FF but still not working in IE7.
<html><head> <script type="text/javascript"> var addDOMMethod = (function () { var ie7 = /*@cc_on!@*/false && typeof HTMLElement == "undefined" && typeof Node == "undefined"; if (ie7) { var methods = {}; return function (that, name, func) { methods[name] = func; document[name] = func; that[name] = (function (func, methods) { return function (arg) { var ret = func.call(document, arg); for (var i in methods) { ret[i] = methods[i]; } return ret; } })(func, methods); }; } else { return function (that, name, func) { if (typeof HTMLElement != "undefined") { HTMLElement.prototype[name] = func; } else if (typeof Node != "undefined") { Node.prototype[name] = func; } if (typeof HTMLDocument != "undefined") { HTMLDocument.prototype[name] = func; } else { document[name] = func; } that[name] = (function (func) { return function (arg) { return func.call(document, arg); }; })(func); }; } })(); addDOMMethod(this, "gid", function(id) { return this.getElementById(id); }); addDOMMethod(this, "gtn", function(tn) { return this.getElementsByTagName(tn); }); var init = function () { var cont = gid("container"); // equal to document.getElementById("container"); var tab = cont.gtn("table"); // equal to document.getElementById("container").getElementsByTagName("table"); alert(tab[0].innerHTML); alert(gid("container").gtn("table")[0].gtn("table")[0].innerHTML); } </script></head><body onload="init();"><div id="container" onload="init();"> <table><tbody><tr><td><table><tbody><tr><td>1267</td></tr></tbody></table></td></tr></tbody></table></div></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:
Expert: iGottZ replied at 2024-12-11 23:02:48
oops. this is supposed to be the valid html. somehow my clipboard didnt copyed the fixed one in:
<div id="container"> <table><tbody><tr><td><table><tbody><tr><td>1232</td></tr></tbody></table></td></tr></tbody></table></div> 1:2:3:
Expert: iGottZ replied at 2024-12-11 23:01:45
most likely because your html is invalid...
yours:
yours:
<div id="container"> <table><tbody><tr><td><table<tr><td>1232</td></tr><table></td></tr></table></tbody></table></div> 1:2:3:
valid:
<div id="container"> <table><tbody><tr><td><table<tr><td>1232</td></tr><table></td></tr></table></tbody></table></div> 1:2:3:
besides that, why are you using gtd? you never wanted a "gtd"
this is what works with my fixed version of your html:
var init = function () { alert(gid("container").gtn("table")[0].gtn("table")[0].innerHTML);"};" 1:2:3:
Author: duncanb7 replied at 2024-12-11 21:01:29
It works for all cases and double(gid().gid()), but the last one I found, for double gtn()
gtd("table")[0].gtn("table")[0].innerHTML is not working if
<div id="container">
<table><tbody><tr><td><table<tr><td>1232</td></tr><table></td></tr></table></tbody></table>
</div>
It will report null.probably is scope in front of gtn().
Would you mind also change that ?
Please advise
Duncan
gtd("table")[0].gtn("table")[0].innerHTML is not working if
<div id="container">
<table><tbody><tr><td><table<tr><td>1232</td></tr><table></td></tr></table></tbody></table>
</div>
It will report null.probably is scope in front of gtn().
Would you mind also change that ?
Please advise
Duncan
Assisted Solution
Expert: iGottZ replied at 2024-12-11 05:09:23
125 points EXCELLENT
IE7 sux. it does not have such a global object wich is used as prototype for each html element..
this solves it:
this solves it:
<!DOCTYPE html><html><head> <script type="text/javascript"> var addDOMMethod = (function () { var ie7 = /*@cc_on!@*/false && typeof HTMLElement == "undefined" && typeof Node == "undefined"; if (ie7) { var methods = {}; return function (that, name, func) { methods[name] = func; document[name] = func; that[name] = (function (func, methods) { return function (arg) { var ret = func.call(document, arg); for (var i in methods) { ret[i] = methods[i]; } return ret; } })(func, methods); }; } else { return function (that, name, func) { if (typeof HTMLElement != "undefined") { HTMLElement.prototype[name] = func; } else if (typeof Node != "undefined") { Node.prototype[name] = func; } if (typeof HTMLDocument != "undefined") { HTMLDocument.prototype[name] = func; } else { document[name] = func; } that[name] = (function (func) { return function (arg) { return func.call(document, arg); }; })(func); }; } })(); addDOMMethod(this, "gid", function(id) { return this.getElementById(id); }); addDOMMethod(this, "gtn", function(tn) { return this.getElementsByTagName(tn); }); var init = function () { var cont = gid("container"); // equal to document.getElementById("container"); var tab = cont.gtn("table"); // equal to document.getElementById("container").getElementsByTagName("table"); alert(tab[0].innerHTML); } </script></head><body onload="init();"> <div id="container"> <table><tbody><tr><td>1232</td></tr></tbody></table> </div></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:
just as sidenote:
in IE7 and below this would work:
gid("container").gtn("table")[0];
but this dont:
document.getElementById("container").gtn("table")[0];
so do not use "document.getElementById() in case you want to use gtn on the returned object.
anyways.. you can also use this:
gtn("table")[0] without a scope in front.
also you could use:
gid("content").gid("some id within it");
have fun. hope this solves your problem somehow.
Expert: iGottZ replied at 2024-12-11 02:10:58
it does not work in ie7?.. hm.. i guess i know why. so far i've only tested it in chrome and ie9.
in some hours i will try making it compatible to ie5 but now i will have breakfast.
in some hours i will try making it compatible to ie5 but now i will have breakfast.
Author: duncanb7 replied at 2024-12-10 19:47:35
Dear IGottz, your code just above is working in all browsers except IE7, Why?
And Proculopsis's code that has done the work, what is advantage of your code
compared to him.Please advise
<script type="text/javascript">
window.onload = function () {
alert( gtn.call( gid("container"), "table" )[0].innerHTML );
};
function gid(id) { return document.getElementById( id ); }
function gtn(tn){ return this.getElementsByTagName( tn ); }
</script>
And I should study more
"this" and ambigous fucntion, (function(func){}(func), HTMLElement, HTMLDocument
term.
And Proculopsis's code that has done the work, what is advantage of your code
compared to him.Please advise
<script type="text/javascript">
window.onload = function () {
alert( gtn.call( gid("container"), "table" )[0].innerHTML );
};
function gid(id) { return document.getElementById( id ); }
function gtn(tn){ return this.getElementsByTagName( tn ); }
</script>
And I should study more
"this" and ambigous fucntion, (function(func){}(func), HTMLElement, HTMLDocument
term.
Expert: iGottZ replied at 2024-12-10 07:36:38
well.. this way it would work without the .call behind addDOMMethod
<!DOCTYPE html><html><head> <script type="text/javascript"> var addDOMMethod = function (that, name, func) { if (typeof HTMLElement != "undefined") { HTMLElement.prototype[name] = func; } else if (typeof Node != "undefined") { Node.prototype[name] = func; } if (typeof HTMLDocument != "undefined") { HTMLDocument.prototype[name] = func; } else { document[name] = func; } that[name] = (function (func) { return function (arg) { return func.call(document, arg); }; })(func); } addDOMMethod(this, "gid", function(id) { return this.getElementById(id); }); addDOMMethod(this, "gtn", function(tn) { return this.getElementsByTagName(tn); }); var init = function () { var cont = gid("container"); var tab = cont.gtn("table"); alert(tab[0].innerHTML); } </script></head><body onload="init();"><div id="container"><table><tr><td>1232</td></tr></table></div></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:
notice: the "this" argument simply refers to the scope in wich the base function should be created in.
calling that function is simply equal to like: document.func but without using document. in coding.
Expert: iGottZ replied at 2024-12-10 07:31:31
waah.. my comment has spelling issues.. made *it just for you
Expert: iGottZ replied at 2024-12-10 07:31:00
<!DOCTYPE html><html><head> <script type="text/javascript"> var addDOMMethod = function (name, func) { if (typeof HTMLElement != "undefined") { HTMLElement.prototype[name] = func; } else if (typeof Node != "undefined") { Node.prototype[name] = func; } if (typeof HTMLDocument != "undefined") { HTMLDocument.prototype[name] = func; } else { document[name] = func; } this[name] = (function (func) { return function (arg) { return func.call(document, arg); }; })(func); } addDOMMethod.call(this, "gid", function(id) { return this.getElementById(id); }); addDOMMethod.call(this, "gtn", function(tn) { return this.getElementsByTagName(tn); }); var init = function () { var cont = gid("container"); var tab = cont.gtn("table"); alert(tab[0].innerHTML); } </script></head><body onload="init();"><div id="container"><table><tr><td>1232</td></tr></table></div></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:
voila.. made is just for you (though i think it can be usefull for my stuff too)
what it does: it extends the DOM with thoose functions and as you can see its a easy to use wrapper.
Author: duncanb7 replied at 2024-12-10 06:08:00
Dear Igottz,
What is different bewteen
HTMLDocument.prototype.gid = HTMLElement.prototype.gid = function (id) {
return this.getElementById(id);
}
And
HTMLDocument.prototype.gid =function (id) {
return this.getElementById(id);
}
In
HTMLDocument.prototype.gid = HTMLElement.prototype.gid = function (id) {
return this.getElementById(id);
}
Meaning do two times of
function (id) {
return this.getElementById(id);
and then save it into HTMLDocument.prototype.gid?
What is different bewteen
HTMLDocument.prototype.gid = HTMLElement.prototype.gid = function (id) {
return this.getElementById(id);
}
And
HTMLDocument.prototype.gid =function (id) {
return this.getElementById(id);
}
In
HTMLDocument.prototype.gid = HTMLElement.prototype.gid = function (id) {
return this.getElementById(id);
}
Meaning do two times of
function (id) {
return this.getElementById(id);
and then save it into HTMLDocument.prototype.gid?
Author: duncanb7 replied at 2024-12-10 06:05:02
Thanks for all of your reply, I am studying this link also, and ECMAScript document
http://odetocode.com/blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx
http://odetocode.com/blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx
Expert: iGottZ replied at 2024-12-10 05:55:00
the this in gtn at this line:
alert(document.gid("container").gtn("table")[0].innerHTML);
refers to the return value of:
document.gid("container")
alert(document.gid("container").gtn("table")[0].innerHTML);
refers to the return value of:
document.gid("container")
Assisted Solution
Expert: iGottZ replied at 2024-12-10 05:53:39
125 points EXCELLENT
<!DOCTYPE html><html><head> <script type="text/javascript"> var gid = function (id) { return document.getElementById(id); } var gtn = function (tn) { return document.getElementsByTagName(tn); } HTMLDocument.prototype.gid = HTMLElement.prototype.gid = function (id) { return this.getElementById(id); } HTMLDocument.prototype.gtn = HTMLElement.prototype.gtn = function (tn) { return this.getElementsByTagName(tn); } function init () { alert(document.gid("container").gtn("table")[0].innerHTML); } </script></head><body onload="init();"> <div id="container"> <table><tr><td>1232</td></tr></table> </div></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:
though that is not IE compatible.. i will add IE compatiblity now.
Author: duncanb7 replied at 2024-12-10 05:51:30
Yes, it works , how gtn will take consider getElementBYid, the element node
before gtn();
I never use call before, could you explain how it works
alert( gtn.call( gid("container"), "table" )[0].innerHTML ); ?
and "this" is referring to what at here ?
Please advise
before gtn();
I never use call before, could you explain how it works
alert( gtn.call( gid("container"), "table" )[0].innerHTML ); ?
and "this" is referring to what at here ?
Please advise
Assisted Solution
Expert: Proculopsis replied at 2024-12-10 05:39:03
125 points EXCELLENT
The main problem is that for this to work you need to call the getElementsByTagName method for the actual object and not document.
<html><head><script type="text/javascript">window.onload = function () { alert( gtn.call( gid("container"), "table" )[0].innerHTML );};function gid(id) { return document.getElementById( id ); }function gtn(tn){ return this.getElementsByTagName( tn ); }</script></head><body><div id="container"><table><tr><td>1232</td></tr></table></div></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:
Assisted Solution
Expert: iGottZ replied at 2024-12-10 05:08:37
125 points EXCELLENT
the issue is that line 4 gets executed before the DOM is ready.
to solve that you could use <body onload="somefunction();">
could place the javascript below the body or could use jquery to use $() to fire as soon as the dom is ready.
besides that..
you are creating thoose functions after you use them. thats not supposed to work fine.
besides that.. what you seek for is appending thoose methods into the DOM objects prototype. i dont know yet how to do that crossbrowser. i will may try it in around a half hour. food is ready.
to solve that you could use <body onload="somefunction();">
could place the javascript below the body or could use jquery to use $() to fire as soon as the dom is ready.
besides that..
you are creating thoose functions after you use them. thats not supposed to work fine.
besides that.. what you seek for is appending thoose methods into the DOM objects prototype. i dont know yet how to do that crossbrowser. i will may try it in around a half hour. food is ready.