Variable exists and remove node (JavaScript)

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Variable exists and remove node (JavaScript)

Post by Wolf_22 »

I'm trying to do 2 things in JavaScript: check to see if a variable exists and remove a node. What am I doing wrong here?

Code: Select all

    
if(!styleNode){
        do stuff...
    }else{
        styleNode = document.removeChild(styleNode);
    }
 
I'm coming from PHP, so my mind urges me to do the above with styleNode, but I'm thinking (from what I found on Google) that I'll need to make a bool function to return true for the exists condition... As for the last part about removing the node, I'm just not sure at all...

Any help is appreciated.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Variable exists and remove node (JavaScript)

Post by it2051229 »

Your code is correct.... post more code.. I think something is wrong with the content of your styleNode variable...
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Variable exists and remove node (JavaScript)

Post by Wolf_22 »

Okay, this may be a naive question, but what's a good way to see what's in a variable in JavaScript?

Usually, in PHP, I would use echo or var_dump, but not in JavaScript...
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Variable exists and remove node (JavaScript)

Post by JellyFish »

Shouldn't this be in the Client Side forum?
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Variable exists and remove node (JavaScript)

Post by Wolf_22 »

I dunno. :dubious:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Variable exists and remove node (JavaScript)

Post by pickle »

Yes, since this is Javascript, it should be in the Javascript forum. I've moved it.

In the future, please take a few seconds to post in the correct forum.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Variable exists and remove node (JavaScript)

Post by kaszu »

Code: Select all

//!styleNode accesses variable value, which will cause an error if variable doesn't exist, but typeof doesn't
if(typeof styleNode == 'undefined'){
    ...
} else {
    //removeChild must be called on elements parent which you want to remove
    styleNode.parentNode.removeChild(styleNode);
}
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Variable exists and remove node (JavaScript)

Post by Wolf_22 »

Okay, so I don't want to dump an entire script off on you guys and expect you to completely unravel all the riddles of its complexities--that would be lazy of me and pretty ignorant, too (and I consider myself fairly hardworking, intelligent young man :) )

So to that end, why not help me understand bits of it, starting with this:
The function "createElement()" creates an element with the specified tag name defaulting namespace depending on the document.
I'm having a hard time understanding exactly what that is supposed to mean. This is partly because whoever wrote the above would fail English, but also because I have no clue what a "namespace" is and how it impacts JavaScript or the use of this function.

Would someone be kind enough to elaborate on this for me? Sorry if I seem arrogant or unappreciative, it's just that teaching yourself this stuff becomes quite the endeavor when the only thing you have to learn from consists of poorly written documentation full of fragments, run-on cliffhangers, and super-technical jargon that only seasoned veterans grasp. :?

I remember hearing about namespacing back in my C++ class days, but it never made sense back then and still doesn't.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Variable exists and remove node (JavaScript)

Post by pickle »

createElement() just creates an element, but it doesn't create it in the DOM, just in the local Javascript namespace. For example:

Code: Select all

function myMakeElement()
{
  var newElement = createElement('TD');
}
This will make a new <td></td> element node, storing it in newElement, which is only accessible from the namespace (otherwise known as "scope") for the function "myMakeElement()"
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Variable exists and remove node (JavaScript)

Post by Wolf_22 »

Thanks pickle, I think I'm following you in this...

createElement() creates elements of markup to be used by JavaScript. It does not create elements of actual JavaScript, right?

One more question and I think I'll have enough to go on for awhile.

Let's say we have your previous example: var newElement = createElement('TD');

What kind of value is newElement holding exactly? The naive side of me says "string" but the more techy side of me wants to say "object." The reason I ask this is because I'm working on a script that creates an element and later does a comparison to see if the href node is empty or has a different sort of value in it.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Variable exists and remove node (JavaScript)

Post by pickle »

I believe newElement will contain an "object"
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Variable exists and remove node (JavaScript)

Post by kaszu »

createElement creates htmlElement (object), but as pickle said it's not inserted in DOM, while it is actual element with all properties any other element would have (except parentNode).
To insert element into dom use appendChild

Code: Select all

var node_div = ...;
var node_a = document.createElement('A');
    node_a.setAttribute('href', 'http://www.google.com');
 
node_div.appendChild(node_a);  //now it is in Dom
Note: node_a.getAttribute('href') for urls like /a.html may return http://domain.tld/a.html in some browsers
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Variable exists and remove node (JavaScript)

Post by Wolf_22 »

kaszu wrote:Note: node_a.getAttribute('href') for urls like /a.html may return http://domain.tld/a.html in some browsers
Lemme guess... Internet Explorer? :x
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Variable exists and remove node (JavaScript)

Post by kaszu »

Lemme guess... Internet Explorer?
As always :)
Luckily there is an easy fix

Code: Select all

function normalizeUrl(href) {
    var domain = document.location.protocol + '//' + document.location.hostname;
    return href.replace(domain, '');
}
Post Reply