user2249657
user2249657

Reputation: 13

When page A loaded outside iframe, redirect to page B and load page A in iframe

Sorry for long title, wasn't really sure how to say it simply.

Anyway, I am making a theme for a blog, and in this theme I am loading some pages in iframes. However, some of these pages are linked to from elsewhere.

So I need to detect when page A is loaded not in an iframe, and redirect to the homepage of my blog. But then I need to open page A inside an iframe on the homepage.

I honestly have no idea how to go about this, or whether it's possible... Is it possible?

(A simple "no that's not possible" is fine. I don't think I have access to any JS libraries or php, and from searching around those are what people have used. I've just got html, css, javascript.)

Upvotes: 1

Views: 558

Answers (1)

DhruvPathak
DhruvPathak

Reputation: 43245

1/ Detect whether it opened without an iframe :

    if ( window.self === window.top ) { 
      redirectAsRequired(window.location);
} 

2/ Redirect as per your requirement :

   function redirectAsRequired(currentUrl)
    {
      var newUrl = page_B_url + ...( ?prev=encodeUriComponent(currentUrl) ; // GET OR POST to the new URL
      window.location = newUrl;
      return;
    }

3/ In page B, get the value of "prev" request variable, and set it as url of the iframe contained in it.

POST for step#2 can be done as shown here : JavaScript post request like a form submit

Upvotes: 0

Related Questions