473,408 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

replaceElement?

Hi,

I have this greasemonkey function:

(function() {
var elementFound = Object();
var tempElem = Object();
var tempText = String("");
var contentString = String("");
var anchorTags = document.getElementsByTagName("a");
for (var i = 0; i < anchorTags.length ; i++)
{
elementFound = anchorTags[i];
contentString = elementFound.textContent.replace(/^\s*(.*)\s*$/,"$1");
//alert(contentString);
if (contentString == "<< Previous") {
elementFound.setAttribute("accesskey","P");
tempText = document.createTextNode("<< ");
elementFound.replaceChild(tempText,elementFound.fi rstChild);
tempElem = document.createElement("b");
tempText = document.createTextNode("P");
tempElem.appendChild(tempText);
elementFound.appendChild(tempElem);
tempText = document.createTextNode("revious");
elementFound.appendChild(tempText);
} else if (contentString == "Next >>") {
elementFound.setAttribute("accesskey","N");
tempElem = document.createElement("b");
tempText = document.createTextNode("N");
tempElem.appendChild(tempText);
elementFound.replaceChild(tempElem,elementFound.fi rstChild);
tempText = document.createTextNode("ext >>");
elementFound.appendChild(tempText);
} else if (contentString == "[Gallery Index]") {
elementFound.setAttribute("accesskey","I");
tempText = document.createTextNode("[Gallery ");
elementFound.replaceChild(tempText,elementFound.fi rstChild);
tempElem = document.createElement("b");
tempText = document.createTextNode("I");
tempElem.appendChild(tempText);
elementFound.appendChild(tempElem);
tempText = document.createTextNode("ndex]");
elementFound.appendChild(tempText);
};
}
})();

Now, this works, but it is ugly as hell. What I would like to do is to
use function fixElement, which would look something like this:

function fixElement(rootElement,beforeText,accKey,afterText ) {
var tempElem = Object();
var tempText = String("");
rootElement.setAttribute("accesskey",accKey);
tempText = document.createTextNode(beforeText);
// I know, I should test on empty beforeText parameter, but
// for the sake of simplicity, let's keep it as it is.
rootElement.replaceChild(tempText,rootElement.firs tChild);
tempElem = document.createElement("b");
tempText = document.createTextNode(accKey);
tempElem.appendChild(tempText);
rootElement.appendChild(tempElem);
tempText = document.createTextNode(afterText);
rootElement.appendChild(tempText);
return(rootElement);
}

Now, the problem is how to call this function. Can I do something like
this?

if (contentString == "<< Previous") {
tmpAnchorElement = fixElement(elementFound,"<< ","P","revious");
elementFound = tmpAnchorElement;
}

Somehow it seems not a good idea. However, I do not know how to get to
the parent element (so that I could use replaceChild). Any ideas?

Thanks,

Matej

Feb 7 '06 #1
7 4424
Matej wrote:
Hi,

I have this greasemonkey function:

(function() {
var elementFound = Object();
var tempElem = Object();
var tempText = String("");
var contentString = String("");
There is not much point declaring variables and initialising them with
dummy content. They don't have a type, so just declare them or
initialise them with real data:

var elementFound;
var tempElem;
var tempText;
var contentString;
If you really insist on initialising them, then:

var elementFound = {};
var tempElem = {};
var tempText = '';
var contentString = '';

does the same job as the original with less typing (but it is still
futile other than from a 'self documenting code' perspective). No
biggie, just a matter of preference really. :-)

var anchorTags = document.getElementsByTagName("a");
Here is a standard initialiser.

for (var i = 0; i < anchorTags.length ; i++)
{
elementFound = anchorTags[i];
See? elementFound was initialised as an Object object, but now it's an
Array object.
[...]
Now, this works, but it is ugly as hell. What I would like to do is to
use function fixElement, which would look something like this:

function fixElement(rootElement,beforeText,accKey,afterText ) {
var tempElem = Object();
var tempText = String("");
rootElement.setAttribute("accesskey",accKey);
tempText = document.createTextNode(beforeText);
// I know, I should test on empty beforeText parameter, but
// for the sake of simplicity, let's keep it as it is.
rootElement.replaceChild(tempText,rootElement.firs tChild);
tempElem = document.createElement("b");
tempText = document.createTextNode(accKey);
tempElem.appendChild(tempText);
rootElement.appendChild(tempElem);
tempText = document.createTextNode(afterText);
rootElement.appendChild(tempText);
return(rootElement);
}

Now, the problem is how to call this function. Can I do something like
this?

if (contentString == "<< Previous") {
tmpAnchorElement = fixElement(elementFound,"<< ","P","revious");
elementFound = tmpAnchorElement;
}

Somehow it seems not a good idea.
This seems like a reasonable place to use innerHTML. Guessing that your
HTML is something like:

<a ...><< Previous</a>
Why not just:

function fixElement(rootElement,beforeText,accKey,afterText )
{
rootElement.innerHTML = beforeText + '<b>' + accKey
+ '</b>' + afterText;
}

And you're done. It's only going to be run in Greasemonkey-compatible
browsers and isn't for the general web so go for it.

However, I do not know how to get to
the parent element (so that I could use replaceChild). Any ideas?


If nodeRef is a reference to a node, then nodeRef.parentNode is it's parent.

--
Rob
Feb 8 '06 #2
RobG wrote:
for (var i = 0; i < anchorTags.length ; i++)
{
elementFound = anchorTags[i];


See? elementFound was initialised as an Object object, but now it's an
Array object.


Actually ... is this true? Isn't the element of Array of the type Object?

Matej

--
Matej Cepl, http://www.ceplovi.cz/matej/blog/
GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC

I am a Roman Catholic, so that I do not expect `history' to be
anything but a `long defeat' -- though it contains (and in
a legend may contain more clearly and movingly) some samples or
glimpses of final victory.
-- J.R.R. Tolkien
Feb 8 '06 #3
ce****@gmail.com wrote:
RobG wrote:
for (var i = 0; i < anchorTags.length ; i++)
{
elementFound = anchorTags[i];


See? elementFound was initialised as an Object object, but now it's an
Array object.

Actually ... is this true? Isn't the element of Array of the type Object?


Yes. Try:

var a = new Object();
var b = new Array();
alert(a.constructor + '\n' + b.constructor);
In JavaScript nearly everything is an object - the ECMAScript spec lists
a number native objects, including Object objects (section 15.2) and
Array objects (section 15.4). An array is a specific type of object
with a special 'length' property and a number of built-in methods (join,
split, concat, etc.).

I guess my point was that the variable itself is not considered to have
a type; trying to give it a type by assigning a dummy value (e.g. an
empty object) as the value does not do anything useful most of the time.

It is useful when initialising variables sometimes, e.g. if you want to
use the compound '+=' operator then:

var a = 0;
a += 7;

seems reasonable (ignoring the trivialness of the example).
--
Rob
Feb 8 '06 #4
RobG wrote:
However, I do not know how to get to
the parent element (so that I could use replaceChild). Any ideas?


If nodeRef is a reference to a node, then nodeRef.parentNode is it's parent.


Well, I know that, but nodeRef.parentNode.replaceChild(...) would not
necessarily replace nodeRef, because there may be other children of
parentNode, would it?

Matej

Feb 8 '06 #5
Matej wrote:
RobG wrote:
> However, I do not know how to get to
> the parent element (so that I could use replaceChild). Any ideas?


If nodeRef is a reference to a node, then nodeRef.parentNode is it's
parent.


Well, I know that, but nodeRef.parentNode.replaceChild(...) would not
necessarily replace nodeRef, because there may be other children of
parentNode, would it?


No, it would not. However,

nodeRef.parentNode.replaceChild(..., nodeRef);

always replaces the Node object referred to by `nodeRef' with the Node
object referred to by `...'.
PointedEars
Feb 8 '06 #6
RobG wrote:
I guess my point was that the variable itself is not considered to have
a type; trying to give it a type by assigning a dummy value (e.g. an
empty object) as the value does not do anything useful most of the time.

It is useful when initialising variables sometimes, e.g. if you want to
use the compound '+=' operator then:

var a = 0;
a += 7;

seems reasonable (ignoring the trivialness of the example).


OK, that makes sense. Thanks!

Matej
Feb 9 '06 #7
Thomas 'PointedEars' Lahn wrote:
No, it would not. However,

nodeRef.parentNode.replaceChild(..., nodeRef);

always replaces the Node object referred to by `nodeRef' with the Node
object referred to by `...'.


Yeah, thanks! At least it shows clearly what newbie I am :-)

Matej
Feb 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rach | last post by:
I just started to learn C++. I copied the following code from a data structure textbook to a ".h" file and couldn't compile it. The code contains three template interfaces. One inherits another. The...
0
by: dfa_geko | last post by:
Hi All, Just had a question about the RSACryptoServiceProvider class. I'm kind of a newbie at this. In the following code at the end of the message, does the key get stored in the User...
8
by: deaddog4201 | last post by:
Hey all. I'm working on an xml file that pulls some data from another xml document "a password, usename and account info" wich is stored on my local drive for easy access so i dont have to enter the...
2
by: Jeyachandiran | last post by:
hi all, whai is the purpose of the replaceellement() regards, jai
10
by: ellie2905 | last post by:
Hello, I am new to this forum and I am glad I found it because it seems that it will help me with my problem.I have creates a site using jsf components like grid panels and buttons.In the mozilla...
1
by: Elliot | last post by:
When decrypt the xml, output "Unable to retrieve the decryption key." Can anyone help me solve the problem? I got the code from http://msdn.microsoft.com/en-us/library/ms229746.aspx using...
5
by: Silgd1 | last post by:
Hey All.... I am creating web page using Netbeans 6.0. I use A jspf (java server page fragment) that is included in most of the other pages. There are several hyperlinks on the jspf that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.