3

I've I'm getting a href value from a loaded AJAX page: and every time I load an AJAX page, it gets the value from the newly loaded page.

I use this to get the right href value:

firstURL = l[0].childNodes[0].attributes[0].nodeValue;

However, on the last page, this node does not exist (because we're on the last page), and returns:

Uncaught TypeError: Cannot read property 'attributes' of undefined trans.js:393
(anonymous function) trans.js:393
j jquery.min.js:2
k.fireWith jquery.min.js:2
x jquery.min.js:4
(anonymous function)

Is there a way for me to assign this variable, if the child node l[0].childNodes[0].attributes[0].nodeValue exists?

2
  • var firstURL = l[0].childNodes[0] ? l[0].childNodes[0].attributes[0].nodeValue : null; Commented Apr 18, 2014 at 13:10
  • Actually using jQuery this would be much easier to solve for jQuery is covering situations when there aren't any matching structures to descend into any further (by handling empty sets of matches). Commented Apr 18, 2014 at 13:28

4 Answers 4

2

You can check if there is a child node before trying to access it:

var firstURL = '';

if(l[0].childNodes.length > 0){ // only if there's more than 1 child node
    firstURL = l[0].childNodes[0].attributes[0].nodeValue;
}

Side notes:

  • childNodes includes text nodes so you might prefer children which only contains elements and no text nodes. If in future you add any text before the target, you will grab the text node instead.
  • You might prefer to use getAttribute('href') instead of getting the first attribute that exists.

To use .getAttribute() you would just replace the .attributes[0] like so:

firstURL = l[0].childNodes[0].getAttribute('href');

This would be more robust because if you ever add new attributes to the element, this will survive the change, whereas relying on it always being the first could cause problems.

Also note that if you use getAttribute() then you don't need to access the nodeValue, because the actual attribute value is returned directly by getAttribute()

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Could you elaborate on how I'd use getAttribute('href') in this instance?
Use firstURL = l[0].childNodes[0].getAttribute('href');
1

You should check to see if l[0].childNodes[0] exists before you dereference it.

if(l[0].childNodes[0]) {
    firstURL = l[0].childNodes[0].attributes[0].nodeValue;
} else {
    firstURL = <default value>;
}

Or as a ternary:

firstURL = (l[0].childNodes[0]) ? l[0].childNodes[0].attributes[0].nodeValue : <default value>;

Comments

0
if(!!l[0].childNodes[0]) {
    firstURL = l[0].childNodes[0].attributes[0].nodeValue;
}

1 Comment

PLease elaborate on your answer.
0

Using jQuery (as tagged in question) this might become quite easy:

var href = $('a.yourlink').attr('href');

This might be null on last page due to $('a.yourlink') not matching any DOM element. But it won't throw any JS exception. So to have some default on last page you might extend that snippet like this:

var href = $('a.yourlink').attr('href') || 'my-default-url.html';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.