Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check whether anchor tags within an iframe contain a URL
    text
    copied!<p>I'm working on a page which is dynamically generated via a CGI system, where the site's real developer doesn't have the time to make changes to the python code for me. That said, I'm forced to frame the page (same domain) and use some JavaScript to modify its contents... Right now, I'm trying to find anchor tags which contain a URL and set the target attribute to "_top". This way, anchors on the page which open menus etc are loaded within the frame, and anything that points offsite is loaded in the overall window. Here's what I've tried:</p> <pre><code>function pointOutside(){ var anchorS = window.frames[0].document.getElementsByTagName("a"); for(i=0;i&lt;anchorS.length;i++){ var foo = anchorS[i].getAttribute('href'); foo = foo.toString(); if(foo.search(/http/i) != -1){ anchorS[i].setAttribute('target','_top'); }; }; }; setTimeout("pointOutside()",2000); </code></pre> <p>The timeout gives the frame's contents sufficient time to load before it's called, but where I'm running into trouble is checking the href property for whether or not it starts with "http". I initially tried this without the "foo = foo.toString();" line, but was getting an error along the lines of "Uncaught TypeError: Cannot call method 'search' on null." I thought to myself, "aha, I'll make sure it's a string!" but no luck. Now I'm getting a similar error when I call toString()... "Uncaught TypeError: Cannot call methor 'toString' of null."</p> <p>The odd thing is that, to make sure my selector was working, I used "alert(foo)" and was able to get a popup for each anchor tag showing the value for the href property. </p> <p>Not sure where I'm going wrong here. Any help would be MUCH appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload