Open all links in parent when page is open inside iframe

0 votes
asked Apr 10, 2017 by anonymous

I have web app where certain portion of it require to be opened inside iframe within the application, i have simple anchor tags populating programmatically.

Is there any good/bad/dirty/quick solution using JavaScript/jQuery that let users open links from iframe to parent window without directly changing any thing programatically or HTML.

  • Challenge i don't want to change any thing in html or server side code, because these anchor tags are generated programmatically.

1 Answer

0 votes
answered Apr 10, 2017 by anonymous

Using simple JavaScript , first check if page is opened inside iframe , then using javascript or jquery make all link target to parent.

if ( self !== top ) {
    // page is in iframe
    $('a[href]').each(function() {
        if (this.getAttribute('href').indexOf('/') === 0 || === '' ) {
   = "_parent"


if( window.self !== ) {/*page is in iframe*/}