What is the code meant? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to remove the cookie from the site. Somebody told me to use this code. But I do not know the functionality of the code. Please tell me.
Here ref is the cookie name which is generating in the site.
var referrer = document.referrer; //returns the URL of the document that loaded the current document.
if (referrer.indexOf(location.host) == -1) {
console.log('1');
jQuery.removeCookie('ref');
if (jQuery.cookie('ref') === undefined) {
jQuery.cookie('ref', referrer, {
expires: .5,
path: '/'
});
referrer = jQuery.cookie('ref');
console.log('2');
} else {
console.log('3');
jQuery.removeCookie('ref');
referrer = jQuery.cookie('ref');
}
} else {
console.log('4');
referrer = jQuery.cookie('ref');
}

When a user to your website, say:
http://example.com/
Now, the document.referrer is "" (blank) as it was not "referred" by any link. The user typed it. The document.referrer holds the link from which the page has been opened.
Now, when the home page has a link like http://example.com/signup, and the user clicks it, and goes to the page, and the page has the following code:
document.referrer; // This would give http://example.com/ as the referrer.
When you are checking this:
referrer.indexOf(location.host) == -1
What exactly happens is:
"http://example.com/".indexOf("http://example.com/") == -1
Where, both have the content. This shows that the link has been clicked from the local page available in the same domain. When the referrer is not the local page, then add the referrer in the cookie and do some process is what the if condition does.
There are a lot of conditions in the code. They are the cases from where the user might have come from.
Case 1: User has not come from anywhere. It returns undefined. You will get 2 logged in the console.
Case 2: If he comes from a different page, You will get 3 logged in the console.
Case 3: If he comes from the same page, You will get 4 logged in the console.

Related

how to restrict access to a webpage if previouse page was not a specific page

I have a QR Code that brings users to PAGE A that immediately forwards to PAGE B. I want Page B to check if users came from PAGE A otherwise forwards them to PAGE C (basically say you need visit PAGE A first). The end result is that Scanning the QR Code allows access to PAGE B through PAGE A but someone can't start on PAGE B without previously being on PAGE A (unless they type it in) It's not foolproof by any means, but it's a deterrent.
On Page B I am using:
if (history.back!="[page a url]") {
location.assign("[page c url]");
}
but this doesn't seem to work.
BTW I'm hacking my way through learning any of this by trying to learn what I need to do what I need to do - please assume I know very little
By your tags, I see that you are using WIX. I'm not sure if you have access to server side there so my answer is in javascript. This isn't full proof as you can't trust anything sent from the browser.
On page A, use localStorage to set a variable visitedA to 1 then redirect to page B.
<script>
localStorage.setItem("visitedA","1");
location.href = "pageB.html";
</script>
On page B check if visitedA equals 0, then redirect to C.
<script>
let visitedA = localStorage.getItem("visitedA") || "0";
if(visitedA == "0"){
location.href = "pageC.html";
}
</script>
Using Document.referrer is a good and most straightforward solution to this problem.
https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer
example:
<script>
// did user come from page a?
if (document.referrer.includes('page-a') {
// yes page-a was in the referrer
} else {
// no, referrer was empty or did not include page a
}
</script>

On one click open 2 pages but not simultaneously [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
ok I want a help with a code, when user click the "download", it should go to the first link but if link 1 is down, it should go to 2nd link ........link 1 should be a default, it should only send the visitor to 2nd link if 1st link dead or down
please tell me if this kind of thing is possible, or just my imagination
and it will be great if the 2nd link is hidden which can't found out by simple inspect tool,if not possible just forget the last line
You can make a call and check the return status with AJAX. Then based on the status code such as 200,404, you can decide what you want to do. This can be done easier with jQuery.ajax() method if you use jQuery.
One of the approach would be to check the URL and recieve the status with AJAX. Based on the returned status code (example 404), you decide what to do next:
with jQuery
$.ajax({
type: 'HEAD',
url: 'http://yoursite.com/pagename.php',
success: function() {
// NO 404 ERROR
},
error: function() {
// error in HEAD (404)
}
});
with Pure Javascript:
function checkUrl(url) {
var request = false;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest;
} else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHttp");
}
if (request) {
request.open("GET", url);
if (request.status == 200) { return true; }
}
return false;

How to redirect to multiple url's using from javascript?

I have a requirement to clear the cookies and to redirect to a new url after clicking on a link. I wrote the below code which calls "logOffRedirectUrlForCookieDeletion" which is responsible to clear the cookies and redirect the application to the homepage and "myUrlAfterCookirDeletion" is the new url that needs to be redirected from the homepage after cookie deletion. If I use any one of the "window.location.href" statements, my code works absolutely fine but if I use two "window.location.href" statements only the second one is getting executed. Can someone please suggest me how to use 2 url redirection using window.location.href
ClearCookiesAndRedirect:function(redirectURL){
if (confirm("Do you want to leave the website?") == true) {
window.location.href = logOffRedirectUrlForCookieDeletion;
window.location.href = "myUrlAfterCookirDeletion";
} else {
return;
}
},
Add an iframe to delete cookies. That would load the cookie deletion page + not redirect, after that redirect:
iframe=document.createElement("iframe");
iframe.src="cookiedeletion.html";
iframe.onload=function(){
//cookies deleted lets redirect
window.location.href="newurl";
};
document.body.appendChild(iframe);
However, why not delete the cookies right on the page? Much easier...

Redirect users who already accepted terms to another webpage (cookies or localStorage)

I ve made a mobile version of a website. Before the user see a specific page he must press a button - CONFIRM, which basically confirms that he accepts the terms of our website. Moreover, as soon as the user press the CONFIRM button he is being redirected to the specific page.
My question is how is it possible to use cookies or localStorage in order to redirect the users who already had been accepted the terms to the specific web page?
I am not quite programming savvy so please be nice :)
You can check like this:
if (localStorage.getItem('accepted') === '1') {
//redirect - already accepted;
}
// after clicking accept
if (accepted === true) {
localStorage.setItem('accepted', '1');
//redirect
}
Note that the check and set must be in the same origin.
Demo:
http://jsfiddle.net/AbdiasSoftware/wk9bS/
Of course, you'll need to check that localStorage is available (implicit IMO) and provide a fallback mechanism such as persistent cookies:
if (!!window.localStorage) {
//use code above
} else {
//use fallback
}

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