www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript redirect based on current url and if in iframe or not

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Question Javascript redirect based on current url and if in iframe or not

    Hi there,

    I am having some issues with redirects (caused by lack of knowledge in Javascript ). What I need to do is to redirect to a new page if the visited URL contains one of the following three words: create, status, home. This redirection may only take place if the visited page is not in iframe.

    Example:

    1. Visitor comes to http://company.hosted.com/create and are redirected to http://www.company.com/create-ticket which has the "http://company.hosted.com/create" page iframed.

    2. Visitor comes to http://company.hosted.com/status and are redirected to http://www.company.com/status which has the "http://company.hosted.com/status" page iframed.

    3. Visitor comes to http://company.hosted.com/home and are redirected to http://www.company.com/support which has the "http://company.hosted.com/status" page iframed.

    4. Visits to all other urls at http://company.hosted.com that do not contain these tree words mentioned should not be redirected (example would be unique urls created for password reset).

    5. If visitor goes directly to our company site which has one of the above mentioned urls iframed then the redirect script in the iframed page should not redirect

    Hope this is not too confusing and that someone is able to help me out

    Thanks,
    Vidir

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    Check if window==window.top to see if you're in a frame or not. window.top is always the top window/frame.

  3. #3
    Join Date
    Oct 2012
    Posts
    2
    Hi,

    sounds good with the iframe part.

    for the redirect part I have tried the following without luck (I have added your part into it):


    var loc = window.location.href;

    if(loc == 'http://company.hosted.com/create' && window == window.top) {
    window.location = 'http://www.company.com/create-ticket';
    }

    if(loc == 'http://company.hosted.com/status' && window != window.top) {
    window.location = 'http://www.company.com/status';
    }

    if(loc == 'http://company.hosted.com/home' && window != window.top) {
    window.location = 'http://www.company.com/support';
    }

    The thing is if the visited URLs do not start with company.hosted.com/ followed by one of these three words (create-ticket, status or support) then the redirect should not take place. With the code above, nothing happens.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles