Open all links in parent when page is open inside iframe

0 votes
63 views
asked Apr 10 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 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 || this.host === 'mydomain.com' ) {
            this.target = "_parent"
        }
     });
}

or

if( window.self !== window.top ) {/*page is in iframe*/}
Welcome , qPocho is an online community for programmers share there knowledge.
...