Javascript to detect domain and redirect - javascript

I'm hoping someone can help me, this HAS to be possible.
My work uses Shopify to run three ecommerce stores for three separate brands.
To make things easier for many reasons operationally I have merged the three stores into one.
I need to redirect the existing brand domain names to the appropriate page in the new store. For example www.brandnameone.com.au needs to go to www.shopifystore.com.au/?view=brandnameone
What I'd like to do is detect which domain name the customer is coming from and redirect them.
Is anyone able to help me out with the script beyond
if(document.domain == "brandnameone.com.au")
?
Thanks so much!

You can do window.location = 'http://URL'; to redirect someone in javascript

This is pretty trivial. You can use window.location.hostname to get the hostname of the current page (source). and redirect using window.location.replace to redirect (source).
switch(window.location.hostname){
case "brandnameone.com.au":
window.location.replace("www.shopifystore.com.au/?view=brandnameone");
break;
...
}

Related

Loading certain page elements based on IP address

Couldn't quite find this exact question but I may have missed it. I was wondering whats the best way to load certain page elements based on the IP address of the site the visitor is coming from. In other words I only want to load a certain navigation button if the site visitor came from site X.
We are testing some cross-domain navigation on an e-commerce site and I want to provide a link that will get people back to their shopping cart if they navigate away from it to our main site. But I only want the link to show up if the people came from e-commerce site, hence I want to only load the link element if the referring IP address is a certain one.
I found the below code but I'm kind of a php newb so i don't know if this is the best way, or if there is a better way using javascript.
If ($_SERVER[“HTTP_REFERER”] == “ip address X”)
{
echo “<div id=""> Back to shopping cart</div>”;
}
Thanks in advance
First: Note that the referer is NOT reliable. While in most cases it will show where a user came from, you should not DEPEND on it being accurate. Security/privacy software will tamper with the value or suppress it entirely.
That being said: the referer is just a url, so
$url = $_SERVER['HTTP_REFERER'];
$urlparts = parse_url($url); // decompose url into components
$host = $urlparts['host']; // get the hostname
$ip = gethostbyname($host); // do DNS lookup for hostname->ip
if ($ip == '127.0.0.1') {
echo "Hey, you must be sitting next to me!"
}

Redirect based on last visited website

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

Redirect if not coming from a referring domain

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!

Javascript Refresh One Time Without Utilizing Hashtags

I'm using an orgy of javascript functions and scripts and they're conflicting a bit.
I have a page which already utilizes a hashtag in order to function correctly but one of the functions doesn't work unless the page is refreshed.
I've done a bit searching and have found a few ways to refresh the page but it checks to see there is a hashtag already in the url... and in my case I already do so it doesn't help.
I want to be able to refresh the page ONE time and that's all... without checking to see if there are hashtags in the url.
Help would be appreciated. Thanks
This might be what you are looking for:
function doRefreshOnce(){
if(localStorage["refreshed"]=="false"){
localStorage["refreshed"]="true"
location.reload();
}
}
What do you mean by once?
Once ever? Once per user? Once per visit? What if a user has two tabs with the same page open? should it refresh in one of those tabs or both?
if you need once ever that needs to be handled server side.
once per user could be handled using local storage or a cookie
once per "visit" (i.e. once per set of tabs) could be handled by a cookie
once per tab/visit needs to be handled by the URL, be that using fragment identifiers or a querystring.

How to get previous page URL in javascript?

How can I design a function that will track the previous page a user has viewed. It will be should be designed to be called on each page.
I will be using a cookies for sure and also they should expire at the end of the visit.
How to achieve that ? I am new to JavaScript :(
If you can explain why you need it, we may find a better solution.
You can use histroy.back() function, i think this would help you.
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_his_back
Document referrer will do this, you can then choose to do what ever you want with this url.
document.referrer

Categories

Resources