1

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.)

1 Answers1

0

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

Community
  • 1
  • 1
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • @user2249657 Yes it aint.That was pseudo code to show that you can send 'prev' URL as a get paramter, you can do it as POST as well as mentioned in the link. – DhruvPathak Apr 17 '13 at 10:47
  • Ahh, okay. So if I use var newUrl = "/" + ?prev=encodeUriComponent(currentUrl); then it will direct to (place).com/?prev=things From my very simple understanding of things that should work, but it does nothing... – user2249657 Apr 17 '13 at 11:30
  • I solved this soon after, sorry for not updating. The problem is that you need encodeURIComponent, not encodeUriComponent. – user2249657 Sep 10 '15 at 14:41