/    Sign up×
Community /Pin to ProfileBookmark

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.

[I]Example:[/I]

  • 1.

    Visitor comes to [url]http://company.hosted.com/create[/url] and are redirected to [url]http://www.company.com/create-ticket[/url] which has the “http://company.hosted.com/create” page iframed.

  • 2.

    Visitor comes to [url]http://company.hosted.com/status[/url] and are redirected to [url]http://www.company.com/status[/url] which has the “http://company.hosted.com/status” page iframed.

  • 3.

    Visitor comes to [url]http://company.hosted.com/home[/url] and are redirected to [url]http://www.company.com/support[/url] which has the “http://company.hosted.com/status” page iframed.

  • 4.

    Visits to all other urls at [url]http://company.hosted.com[/url] 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

    to post a comment
    JavaScript

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @ReFreezedOct 27.2012 — Check if window==window.top to see if you're in a frame or not. window.top is always the top window/frame.
    Copy linkTweet thisAlerts:
    @vidirkauthorOct 27.2012 — 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.
    ×

    Success!

    Help @vidirk spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...