Redirect based on referrer URL - javascript

In my site I have a password protected page containing some links to other sites also operated by myself that cannot be password protected. I would like to place a HTML code onto one of the other sites I operate that checks that the person arriving at the page has been referred from the URL of the 'Links Page'.
(I understand that this is not a secure option)
Summary:
If Referrer = 'Links Page URL' *then* Do nothing *Else* Redirect: www.google.com.
Does anyone know a simple HTML/ Javascript code that I can copy and paste into my site?

if (document.referrer !== "http://www.stackoverflow.com") {
window.location.href = "http://www.google.com";
}
Or you can use regular expressions to check the referrer.
Anyway, this solution is really, really unsafe. You can just turn off JavaScript in your browser and won't be redirected...

Try this
function url(url){
return url.match(/:\/\/(.[^/]+)/)[1];
}
function check()
{
var ref = document.referrer;
if(url(ref) =='www.google.com')
{
// do something
}
else
{
// redirect
window.location.href = 'http://yourDomain.com';
}
}

I found document.referrer doesn't work for me, but location.href works:
if (location.href != "http://yoursite/index.html") {
location.replace("http://yoursite/index.html");
}

Related

Trying to redirect a page depending on what is in the URL with Javascript

I am looking to redirect a webpage if the URL is a certain URL. I can only edit the header script (which is in every webpage), but I only need to redirect the one page, so I chose to go this route.
For example, if the URL is /blog I want it to redirect to /blog-home. The problem I am having is that /blog/blogpost is also redirecting to /blog-home. I only want the one page (/blog) to redirect.
Right now I have:
if(document.URL.indexOf("/blog") >= 0){
window.stop();
window.location.href = "/blog-home";
}
With this code, /blog redirects fine to /blog-home.
You can use endsWith to make sure that '/blog' is the last part of the URL.
if(document.URL.endsWith("/blog")){
window.stop();
window.location.href = "/blog-home";
}
You can check it using a regex, consider also that it might be an additional trailing / after the url, it might end for example with /blog or /blog/:
if(document.URL.match(/\/blog\/?$/) !== null){
window.stop();
window.location.href = "/blog-home";
}
Split the URL on / characters and use an equality test on the last part.
var split = document.URL.split("/");
if (split[split.length-1] == "blog") {
window.stop();
window.location.href = "/blog-home";
}
window.location gives you the current url path
window.location.origin gives you the base path thus you do the following :
if(window.location === window.location.origin +"/blog" ) {
window.stop();
window.location.href = "/blog-home"
}

Attempting window.location or window.location.href redirect which is causing a loop

I'm attempting to use javascript to determine if the user is using a certain language and if they're not using english then for the page to load a different page BUT with the params of which I've grabbed from the url.
I have been able to load the page with the params but I keep falling into a loop reloading the page, even after skimming through the countless other examples, such as: this or this.
function locateUserLanguage() {
var languageValue = (navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage)).split('-');
var url = window.location.href.split('?');
var baseUrl = url[0];
var urlParams = url[1];
if (languageValue[0] === 'en') {
console.log('no redirect needed, stay here.');
} else {
// I tried to set location into a variable but also wasn't working.
// var newURL = window.location.href.replace(window.location.href, 'https://www.mysite.dog/?' + urlParams);
window.location.href = 'https://www.mysite.dog/?' + urlParams
}
} locateUserLanguage();
I've attempted to place a return true; as well as return false; but neither stop the loop.
I've tried window.location.replace(); and setting the window.location.href straight to what I need, but it's continuing to loop.
There is a possibility that the script in which this function is written is executed in both of your pages (english and non-english) on load. So, as soon as the page is loaded, locateUserLanguage function is executed in both english and non-english website causing the infinite loop.
You need to put a check before you call locateUserLanguage function.
Suppose english website has url = "www.myside.com" and non-english website has url "www.myside.aus". So the condition needs to be
if (window.location.host === "www.myside.com") { locateUserLanguage() }
This will make sure that locateUserLanguage is called only in english website.
Or other apporach can be to load this script only in english website which will avoid the usage of conditional statement.
Hope it helps. Revert for any doubts.

How to redirect to another url on refresh using JavaScript/(other easy alternatives)?

How to redirect to another url on page refresh or page back using JavaScript or (any other easy alternatives );
You can combine the page capture via something like a navigator object shown here:
https://stackoverflow.com/a/36444134/3187487
And then use window.location to redirect the person to where ever you want from there.
You can do something like this perhaps:
if (window.performance) {
console.info("window.performance works");
}
if (performance.navigation.type == 1) {
window.location.href = "https://www.example.com"
}
Ref1, Ref2

URL redirect, but can't get it to save in browser history.

The code I've included below is meant to redirect to a new url if the user is not on a specific page or does not have a certain cookie. The cookie function works perfectly as does the redirect. Here is my problem: The window url redirects, but the original url is not logged in my browser history.
<script type="text/javascript">
$(document).ready(function() {
if (getCookie('legal_age') == "yes" || window.location =="http://example.com/home") {//user is legal age!
} else {
setTimeout(function() {
window.open('http://example.com/welcome','_self','', false);
},0)
}
});
</script>
For example, if I visit "http://example.com/page1", the browser redirects to "http://example.com/welcome", as it should. However, I need the original url visited ("http://example.com/page1") to show up in my browser history so that I can call upon it in a different function. Here is the code I am using to call the history (within a form):
<form action="javascript:window.location.reload(history.go(-1));" method="get" name="age_form" id="ageForm" />
I've also tried this alternative to call the history and it didn't help:
window.history.back();
I have also tried the following with no success in saving original url in browser history:
window.location = "http://example.com/welcome";
window.location.href = "http://example.com/welcome";
window.location.assign("http://example.com/welcome");
Finally, I included this function because another thread suggested it might help, but it hasn't seemed to do much:
setTimeout(function(){
Any ideas?
Is there anyway to get the original url visited to log in my browser's history before redirecting? HELP please!
setting a cookie of the current URL before redirecting worked well. Here is the altered code that worked. keep in mind that in order to set a cookie, you must reference a javascript cookie function (in this case mine is called setCookie():
<script type="text/javascript">
$(document).ready(function(){
if (getCookie('legal_age') == "yes" || window.location =="http://example.com/home") {//user is legal age!
} else {
setCookie('originalURL', window.location);
var urlCheck = getCookie('originalURL');
window.open('http://example.com/welcome','_self','', false);
}
});
</script>
Hope this helps someone!

Rgd: Javascript redirect top parent page

Edited question
In summary:
I got 2 sites
siteA.com
siteB.com
Iframe belong to siteB.com
But i want allow siteA.com to iframe siteB.com page too. However anything that not siteA or siteB.com will be redirect to siteA.com
using javascript, how do i check, consider to ignore with www or without www(wildcard) and also that my site could be siteA.com/pretty-url
How do i do the check and add in the security with javascript , which any random site not authorize will result in window.top.location.href being redirect to siteA.com
Thanks for all help, new here :)
Something like this?:
if (window.top != window && !window.top.location.href.split('?')[0].split('#')[0].match('mysitedomain.com'))
{
window.top.location.href = window.location.href;
}
The first check is making sure you only run this code if your site is in a frame.
The second check is looking to see if url of the top frame (browser window) contains your domain. We need to ignore the querystring/anchor incase it looks something like this: http://notmine.com/path/file.html?subframe=mysitedomain.com
This would still match:
http: //not*mysitedomain.com*/path
In the .match(...), you could include http:// or https://.
Update to answer your edits:
var topUrl = window.top.location.href.split('?')[0].split('#')[0];
if (window.top != window && !topUrl.match('siteA.com') && !topUrl.match('siteB.com'))
{
window.top.location.href = "siteA.com";
}

Categories

Resources