0

I can't modify the following html:

<iframe src="http://google.com/foo" width="600" height="338" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>
<div style="padding:5px 0; text-align:center; width:600px;"><p>
<a href="http://www.foobar.com/lol/wut">Some Link</a></p></div>

I need to target this element:

<a href="http://www.foobar.com/lol/wut">Some Link</a>

The requirements are:

Since I can't edit the html I can't simply add an id and use document.getElementById. So how can I target the link using just plain js?

4
  • 1
    DOM operations... run through the DOM tree, find the iframe, then look "after" the iframe for the link. Commented Feb 11, 2013 at 20:42
  • What mark said, and check out this answer: stackoverflow.com/a/1088569/877472 I've not really done this all that much, but that answer looks like it might be a good starting point at least. Commented Feb 11, 2013 at 20:43
  • @Mathletics: No, definitely not. Commented Feb 11, 2013 at 20:48
  • @Mathletics the a tag isn't within but after the iframe. Commented Feb 11, 2013 at 20:48

3 Answers 3

2

Check out selectors, especially attribute selectors and sibling combinators:

iframe[src^="http://google.com"] ~ a

You can easily use that in a jQuery selector expression or document.querySelector.

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

1 Comment

I get the error elem is null: var elem = document.querySelector('iframe[src^="http://google.com"] ~ a').innerHTML = 'abc';
0
function aSrcAfterIFrame(document) {
    var elements, result, looking, item, tagName, i, n;
    elements = document.getElementsByTagName('*');
    looking = false;
    for (i = 0, n = elements.length; i < n; i++) {
        item = elements[i];
        tagName = item.tagName.toLowerCase();
        if (looking && tagName === 'a') {
            result = item.href;
            break;
        }   
        if (tagName === 'iframe') {
            if (item.src.indexOf('http://google.com/foo') === 0) {
                looking = true;
            }   
        }   
    }   
    return result;
}

aSrcAfterIFrame(document);
"http://www.foobar.com/lol/wut"

Comments

0

READ EDITS.

Untested but assuming you're using jQuery something like this should do the trick.

var link = null;
$('iframe:first').siblings().each(function() {
    if (link !== null) return;
    if ($(this).attr('href') && $(this).attr('href').search('http://www.google.com') > -1) {
        link = $(this);
    }
});

Not the best solution, however I feel like this is an odd scenario.

EDIT: If Bergis selector works use that. Fancy stuff, I just learned something.

EDIT 2: Also I just noticed you wanted to check the iframe src not the link source so my post is essentially just plain wrong.

1 Comment

I can't use jquery unfortunately, it's part of a analytics script so it has to be plain js for performance. That's actually why I asked the question. I know how to do this in jquery but not plain js.

Your Answer

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