Page 1 of 1

Variable exists and remove node (JavaScript)

Posted: Wed Oct 28, 2009 8:43 pm
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.

Re: Variable exists and remove node (JavaScript)

Posted: Wed Oct 28, 2009 10:04 pm
by it2051229
Your code is correct.... post more code.. I think something is wrong with the content of your styleNode variable...

Re: Variable exists and remove node (JavaScript)

Posted: Thu Oct 29, 2009 7:11 am
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...

Re: Variable exists and remove node (JavaScript)

Posted: Fri Oct 30, 2009 1:51 pm
by JellyFish
Shouldn't this be in the Client Side forum?

Re: Variable exists and remove node (JavaScript)

Posted: Fri Oct 30, 2009 2:03 pm
by Wolf_22
I dunno. :dubious:

Re: Variable exists and remove node (JavaScript)

Posted: Fri Oct 30, 2009 2:09 pm
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.

Re: Variable exists and remove node (JavaScript)

Posted: Fri Oct 30, 2009 7:41 pm
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);
}

Re: Variable exists and remove node (JavaScript)

Posted: Sun Nov 01, 2009 10:35 am
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.

Re: Variable exists and remove node (JavaScript)

Posted: Mon Nov 02, 2009 10:16 am
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()"

Re: Variable exists and remove node (JavaScript)

Posted: Mon Nov 02, 2009 11:14 am
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.

Re: Variable exists and remove node (JavaScript)

Posted: Mon Nov 02, 2009 11:25 am
by pickle
I believe newElement will contain an "object"

Re: Variable exists and remove node (JavaScript)

Posted: Mon Nov 02, 2009 11:27 am
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

Re: Variable exists and remove node (JavaScript)

Posted: Mon Nov 02, 2009 11:37 am
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

Re: Variable exists and remove node (JavaScript)

Posted: Mon Nov 02, 2009 12:43 pm
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, '');
}