mufc
mufc

Reputation: 715

display parent back history in iFrame

I want to display a users previously visited page in an iframe on my page. For example: if a user is on cnn.com, and then they type in mysite.com an click on a button on my page, I was the iframe on my page to display cnn.com.

I have read several solutions online but none have fixed my issue. My current implementation is as follows:

index.html:

<body>
        <div>
            <h1>Welcome to mysite!</h1>
            <h3>Click the button to see a site you have previously visited</h3>
            <button id="button">magic</button>
        </div>
        <br>    
        <p>This is a site you visited previously</p>
        <iframe id="myIFrame" height="500px" width="100%" class="netframe" src="example.com"></iframe>

</body>

script.js:

$(document).ready(function(){

        $("#button").click(function(e){  
            document.getElementById("button").style.color = "red"; 
            $("#myIFrame").attr('src', parent.history.back());

        });
});

The issue I am having is that when I click the button, the entire page goes back, and it is not shown in the iframe. So in my example when I click the button, my entire page goes from mysite.com to cnn.com. Id like the parent page to remain the same, and the iframe to load cnn.com within it.

I have read up on some of the quirks regarding how history is different depending on how you load up an iframe, but none of that has helped me.

If there a way to do what I want to do?

Upvotes: 0

Views: 557

Answers (1)

user149341
user149341

Reputation:

No, this isn't a thing that you can do. The contents of the user's browser history are generally treated as a secret; you cannot cause the browser to reveal them to you.

In situations where the user navigated to your page by clicking a link on the previous page, you can determine what that page was by checking document.referrer. However, this will not be the case if the user entered your page's URL manually, as you describe in your question.

In any case, methods like history.back() do not return a value; they cause the frame to navigate to the previous page in its history. There is no way to determine what this page is, and doing so will generally cause your script to stop running (as your page is no longer active).

Upvotes: 2

Related Questions