-1

I have iframe created and it has no src at the beginning. When I click on a link, for example, the result from Google search, that link should try to open in that iframe. Not in the tab that I am currently browsing.

$(document).ready(function () { 

    $('<div />', {
        id: 'firster',
        style: 'position: fixed; top: 0; bottom: 0; right: 0; max-width: 500px; z-index: 120;'
    }).appendTo('body');

    $('<iframe />', {
            name: 'frame1',
            id: 'frame1',
            src: ''
        })
        .css({
            'height': '100%',
            'width': '450px'
    }).appendTo('#firster');

    $("a").click(function(e) {
        $("#frame1").attr("src", window.location.protocol + '//' + window.location.host +  $(this).attr("href"));
        return false;
    })

});

What I expect is to open that url ( I concated the protocol host and url ), it just does not open.

Element status before clicking on link:

enter image description here

And element status after clicking on link: enter image description here

How to fix this? Any way around it?

Edit part:

This is what im getting periodically in console:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://accounts.google.com/ServiceLogin?passive=1209600&osid=1&continue=https://notifications.google.com/u/0//idv2&followup=https://notifications.google.com/u/0//idv2&authuser=0. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

2 Answers2

0

Kinda worked out what I wanted. For everyone who wants to see its here:

$(document).ready(function () { 

    $('<div />', {
        id: 'firster',
        style: 'position: fixed; top: 0; bottom: 0; right: 0; max-width: 500px; z-index: 120;'
    }).appendTo('body');

    $('<iframe />', {
            name: 'frame1',
            id: 'frame1',
            src: ''
        })
        .css({
            'height': '100%',
            'width': '450px'
    }).appendTo('#firster');

    function loadIframe(iframeName, url) {
        if ( window.frames[iframeName] ) {
            window.frames[iframeName].location = url;   
            return false;
        }
        return true;
    }
    $("a").click(function(e) {
        $("#frame1").attr("src", decodeURIComponent($.urlParam('url', $(this).attr("href")))); // no prepend of current URL, just use the href directly


        return false;
    })

    $.urlParam = function (name, urlItself) {
        var results = new RegExp('[\?&]' + name + '=([^&#]*)')
                          .exec(urlItself);

        return results[1] || 0;
    }

   // console.log($.urlParam('url', urlItself)); //registration

});

I am sure that this will work on Googles search-result links and not quite sure if it is scalable but it works for my original purpose. Also, big thanks to everyone who contributed on this issue.

-1

you can simply achieve it in html

here try this

<iframe height="300px" width="100%" src="" name="iframe_a"></iframe>

<p><a href="https://www.stackoverflow.com" target="iframe_a">stackoverflow.com</a></p>

you cant get to target because. it writes src as "yourwebsie.com+src"

in jquery:

  $(document).ready(function(){
    $("a").click(function(){
    var k=$(this).attr("src"))
        $("iframe").attr("src",k);
    });
});

javascript method:

<script>
 function loadIframe(iframeName, url) {
    if ( window.frames[iframeName] ) {
        window.frames[iframeName].location = url;   
        return false;
    }
    return true;
}</script>

<a href="link" onclick="return loadIframe('ifrm', this.href)">whasjjjf</a>
    <iframe name="ifrm" id="ifrm" src="tabs.html" frameborder="0">
    Your browser doesn't support iframes.</iframe>
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22