Forum Discussion

id-chalmers's avatar
12 years ago

using freemarker to get window.top

Is there a method in the freemarker context object (or other object) that is roughly equivelant to javascript's window.top? I'd like to detect whether my page is being  loaded from within an iframe.  The javascript way to do it is :

 

if (window.self != window.top ){

     //page is embedded in iframe 

}else{

    //not in iframe

}

 

 

3 Replies

  • I'm fairly sure there is no direct way to detect if the page is within an iframe on the server (i.e. with FreeMarker code). However, you may be able to infer it based on the referrer (which is stored in the http.context object: http://lithosphere.lithium.com/t5/developers-knowledge-base/http-request-FreeMarker-context-object/ta-p/9323).

     

    Normally the referrer will give you the URL of the page (internal or external) that linked to the community page currently being requested. However, if the page is embedded in a frame (or iframe) it will normally give you the URL of page containing the iframe. You may be able to use this information to infer whether or not the page is being loaded in an iframe.

     

    Bear in mind that, even with this approach, it is very easy to spoof the referrer.

  • id-chalmers's avatar
    id-chalmers
    Mentor
    12 years ago

    Hi Nathan

    Thanks, that would work if i were only loading the iframe once, however I need to detect when the page is still in the iframe even after the user navigates around the embedded site using internal links. In that case the referrer is no longer from the iframe. 

     

    I guess it's not surprising that freemarker would not have access to the DOM since it operates on the server and has already done its work by the time the window is loaded into the frame. 

     

    Thanks anyway

    Chris

  • nathan's avatar
    nathan
    Executive
    12 years ago

    It's not perfect, but you could always use a cookie to store whether or not the user is browsing within an iframe.

     

    You would set the cookie on the first page the user views (as at that point you can detect that it is in an iframe). Then you can check for that cookie on the other pages.

     

    If you have control of the parent frame, you could actually pass a parameter in the URL to the first community page you load (which will be a bit more robust than checking the referrer).

     

    The disadvantage with this approach is that you can't tell if the user jumps 'out' of the iframe, as the cookie won't change. For example, they might load the community in a separate tab, or just navigate to the community directly.