At the moment i'm trying to make a new script that is based on the last visited website of the user. If last visited website is x, then it needs to redirect to another page.
Example:
User clicks on a link on my Facebook fan-page and lands on the homepage. The Javascript now needs to take an action and redirect the user to another page. But only if the user came from http://facebook.com/[fanpage]
Is it possible to create something like this in Javascript?
Thanks a lot,
You could use document.referrer like this:
if ('http://facebook.com/[fanpage]' == document.referrer) {
location.href = 'new_destination.html';
}
If Facebook doesn't do some kind of referrer blocking, then use document.referrer to get the referring page.
(It sounds like that might be something better accomplished server-side, though, in which case you would use the Referer [sic] header.)
I wouldn't rely on referrers sent by the browser. They can be turned off.
Why don't you pass a special parameter in your URL?
http://www.example.com/?fromFb=1
Related
I'm building a web application and for one of my pages, I need to determine how a user arrived at the page (i.e. by redirect or by directly typing in the URL).
Is this possible with vanilla JavaScript?
document.referrer.
You can also read Referer header on your backend server, because browser automatically includes it.
You can try using document.referrer
If somone types url in address bar it should return an empty string, otherwise if a link was clicked it should return prior page url
You might think this is a duplicate, but it's not. I've seen the other SO answers about changing the browser url with replaceState() but I have a problem...
On one webpage on my website, it receives a lot of parameters, so the link goes on and on and on... I did replaceState(), and it shortened the URL by a lot. That worked. If I go on another link, and I go back in history, the URL will still be the same that I defined on replaceState() and the page gives a 404 error. Is there a way to just load the link the way it is and then temporarily change the URL so the user doesn't see a huge link but the system uses the actual URL?
I'm using JSP, go ahead and give me answers in JavaScript, JQuery, or Java.
You could store the parameters in localstorage (ie a cookie) then have your next page un-pick them from localstorage, thus reducing the size needed of the URL in the frst place. Example code (stolen from Storing Objects in HTML5 localStorage) :
// add to storage
localStorage.setItem('myAttibute', 'acceptable');
// Retrieve the object from storage (on another page)
var sMyAttibute= localStorage.getItem('myAttibute');
alert("myAttibute=" + sMyAttibute);
Hopefully it'll tell you that my attribute is acceptable.
I would like to check if a user who opens my website is new (type in url OR redirect through google) and then display a message.
But when the user browse through the site (subpages like about and so on) this message is not displayed.
But when the user closes the browser and visits (maybe a few mintues later) the website again, the message should be displayed again.
How can I implement something like this in JavaScript?
You could use cookies, localStorage, sessionStorage or the referrer info. Note that all of them have their own pros and cons and there is no 100% reliable way of detecting new visitors.
Using the firstImpression.js library that Nadav S mentioned is probably the easiest way.
To get the message to show up for users closing and reopening the site:
unset your cookie / localStorage data on unload or
use a referrer info or sessionStorage based solution
See these MDN resources for more:
cookie
localStorage
sessionStorage
referrer
Slightly relevant as well: http://www.cookielaw.org/faq/
From the MDN:
Returns the URI of the page that linked to this page.
string = document.referrer;
The value is an empty string if the user navigated to the page
directly (not through a link, but, for example, via a bookmark). Since
this property returns only a string, it does not give you DOM access
to the referring page.
This means:
if (!document.referrer) {
//Direct access
} else {
var referer = document.referrer;
//Request comes from referer
}
If you want to save this state, you need to take a look at cookies.
Quite easily, you want session storage
var hasVisited = sessionStorage.getItem('washere');
if ( ! hasVisited ) {
// do stuff
alert('Welcome, stranger !');
sessionStorage.setItem('washere', true);
}
You can implement this by using cookies.
When the visitor first come to your page, you check if your cookie exist, if not show the message to them and then create the cookie for future pages.
Using cookies is probably your best bet. They are super simple to use too. Just write
if(document.cookie = "HasVisited=true"){
//whatever you want it to do if they have visited
}
else {
document.cookie = "HasVisited=true"
//that just saves to their browser that they have for future reference
}
I know that I can find out the referrer using:
document.referrer;
However, I have a one page website, and a redirection set to send all other pages in that website to the home page. I would like to have a way of capturing the link that originated the redirection. In this case, document.referrer is always empty.
So I guess, I need to know:
How do I set a referrer parameter before the redirection?
How do I capture that parameter with JavaScript in the home page?
You could pass it along in a URL parameter. For example, Google does something similar when you click a search result; the browser actually goes to google.com/url?url=https%3A%2F%2Fthe-site-you-want.com.
So you could redirect your users to 'http://your-site.com/?referrer='+ encodeURIComponent(document.referrer), and then once it hits the homepage, you can extract that value and run decodeURIComponent.
encodeURIComponent is a method that makes a value safe to put in a URL. decodeURIComponent is the reverse process.
Alternatively, you could put it in a hash rather than the querystring, like 'http://your-site.com/#'+ encodeURIComponent(document.referrer). Several client-side routers use this. Although that may break the back button unless you spend more time learning about pushState. When Twitter first used twitter.com/#!/foo-bar as a URL scheme, it broke many things. But it may be useful to you.
I am wanting to set up a javascript to accomplish the following:
A user opens up a bookmarked webpage and the script checks whether or not it came from a referring domain (say, site.com) and if not redirects them automatically to anothersite.com. The goal is so that only when users come from the anothersite.com site will the site.com page come up. Hope this makes sense.
I would normally use PHP for this but can't for this project so we're stuck doing this with javascript.
Look into document.referrer to get the referrer's URL, extract the domain from it and then validate.
I didn't quite get the part of "opens up a bookmarked" since it has been said that knowing that much is not easily accomplished Is there a way to know if someone has bookmarked your website?
..but to inspect where it came from I would look into How do you get the previous url in Javascript?
plus then make your own if else stuff and use window.location.href = "your_site_url"; to direct him the page you wanted, job done!