js redirect if not in fb iframe - javascript

i want to redirect all traffic going to my Facebook app tab on my server directly to my Facebook app. Therefore I check with the following code if the user is inside the Facebook iframe or on my webpage:
<!-- language: lang-js -->
function referrerIsFacebookApp() {
if(document.referrer) {
return document.referrer.indexOf("facebook.com") !== -1;
}
return false;
}
if (!referrerIsFacebookApp()) {
top.location.replace("https://www.facebook.com/bommelME/app_264697733636385");
};
If I open up the page with the browser everything works as it should. But if I link to this page and open the link the redirect doesnt work. Any hints?

Use window.top to detect whether your app is in an iFrame or not. Try this.
if (window!=window.top) {
/* I'm in a frame! */
window.location = "https://www.facebook.com/bommelME/app_264697733636385";
}
Cheers.

I think you should check the window.parent object instead of document.referrer, because the page can be referenced from another one as you've said but not being included via iframe

Related

Javascript mobile redirect, page keeps refreshing

I'm trying to redirect all my site mobile traffic to one page.
so I added redirect code in header.
<script type="text/javascript">
<!-- if (screen.width <= 699)
{ document.location = "/page/mobile/";
}
//-->
</script>
The problem is that after redirecting script continuous executing because header is the same on the whole site. so it's refreshing the page over and over again.
I want javascript to check for the /page/mobile/ in url and if it's there, do not execute redirect.
How can I achieve this? Thanks!
Do this.
if ( window.location.pathname !== "/page/mobile"){
window.location.href = "https://[your-host-name]/page/mobile";
}
So you can use window.location.pathname to access your path after host (and port)
https://developer.mozilla.org/en-US/docs/Web/API/Window/location
check window location in your if condition
if (window.location.href.indexOf("/mobile/")>-1){
console.log("You are on mobile page")
}

Detects Redirect and open a modal (Popup)

I have a scenario trying to redirect DNS to another URL and i.e http://oldexample.com to another page of new website https://example.com.com/newpage
I am trying to make a script that detects traffic to https://example.com/newpage from http://oldexample.com and if it's detects that trigger to open modal/popup/alert message will saying some information on this page (https://example.com/newpage).
I have tried following solution but it does not work? Is it right way to implement it?
$(document).ready(function($) {
var isExternal = document.referrer;
console.log(isExternal);
if (isExternal === "https://oldexample.com") {
// Do something
alert('Referred from expected page. Probably.');
}
});

iFrame Redirect Issue - Not Changing the page

<iframe src="http://runebet.com/" width="80%" height="65%" name="runeBetAPI"></iframe>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
function RegisterPage() {
window.frames["runeBetAPI"].location = "http://runebet.com/register/";
}
RegisterPage();
</script>
This doesn't seem to work/change the content..
The URL should start with http://www. to avoid a server redirection
you can access iframe contents from the same domain only read here.
You can use the following to change the source of the iframe.
function changeSource(src, frameID) {
var frame= $('#' + frameID);
if ( frame.length ) {
frame.attr('src',src);
return true;
}
return false;
}
But apparently what looks like looking into what your code is doing the url that you are redirecting to it actually opens a registration form inside modal window, which for some reason is not opening if you access it via an iframe, because if you put the url http://runebet.com/register/ directly in the iframe and load the page it will still be showing the landing page so it is redirecting but the modal is not popping up that leaves you with the last thing that you have to trigger the Register link click and that can only happen if you are in the same domain if not maybe someone else knows, i dont.
$(window.frames[0].frameElement.contentDocument).find('a[href="/register"]').trigger('click');

How to open a link inside an iframe from a hta in the default browser

What I need is just like How can I open a link in the default web browser from an HTA?, but with the restriction that the link sits inside an iframe.
The iframe's loads a page in our server.
Idea: can the iframe's redirect be detected & prevented, so then we'd run code like in https://stackoverflow.com/a/185581/66372. How?
Update 1
To be clear: the problem we're trying to solve is that when the user clicks on any link, it opens in the default browser.
One option similar to mplungjan's answer, is to capture the click event for all links in the iframe's DOM. Is there a more generic option that works at the iframe, document or body level? (and thus also works with delayed loads and any other tricks)
Something like this, which should be perfectly allowed in an HTA which has elevated rights
window.onload=function() {
window.frames["iframe_in_this_document"].onload=function() {
var links = this.document.getElementsByTagName("a");
for (var i=0;i<links.length;i++) {
url = links[i].href;
if (url) links[i].onclick=function() {
var shell = new ActiveXObject("WScript.Shell");
shell.run(this.href);
return false;
}
}
}

Detect window close command from child (iframe)

I have an iframe in my html page from another website.
User does something in that iframe, and then the page closes itself. (that page isn't for me, I can't change the functionality of that)
I want my html page to detect this close command from child and then, change visiblity of the iframe to false.
How to do that??
Save the current location and use use setInterval to start the loop.
var startingLocation = document.getElementById('myIFrame').location;
setInterval("checkChildLocation",1000);
function checkChildLocation() {
if(document.getElementById('myIFrame').location != startingLocation) {
/* the iframe has changed */
}
}
Or something like that.
I believe this violates the cross origin policy, and cannot be accomplished. Have a look at:
https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript
http://en.wikipedia.org/wiki/Same_origin_policy

Categories