Javascript: Creating a 'weird' conditional re-direct - javascript

If I had a normal website this would be a simple enough fix... but I've built my site on tumblr so I need a workaround. Every page runs off of the same code, so any solution script is going to run on every page.. can't quite figure this one out (did I mention I'm a total n00b?). There are lots of answers to questions LIKE this one, but I couldn't quite find the right syntax I suppose to answer this question...
The goal here is, if some goes to just the raw domain name, in this case milliondollarextreme.tv --> I'd like it to redirect to milliondollarextreme.tv/tagged/videos.
In any other case, by that I mean, if there is anything appended to the end of the domain name already, such as:
milliondollarextreme.tv/permalink/91298132843
milliondollarextreme.tv/tagged/blog
milliondollarextreme.tv/contact.htm
I don't want there to be any redirection going on. I only want the redirect to 'fire' really the first time the person types in the domain -- milliondollarextreme.tv
The trick here, the reason why I am asking (I did a search and 1000 apologies if this has been asked elsewhere, I just couldn't find it) is that the script has to run on every page, because it's hosted on tumblr, so every page is driven by the same code.
Any ideas? Thanks in advance.

This will simply redirect any visit to milliondollarextreme.tv/ to milliondollarextreme.tv/tagged/videos
if(window.location.pathname == '/')
{
window.location.pathname = '/tagged/videos';
}
However, it will do it every time they go to the root; like Gerardo, I'm not clear if that's what you want.

<script>
if( window.location.href == "http://milliondollarextreme.tv" ||
window.location.href == "http://milliondollarextreme.tv/" ||
window.location.href == "http://www.milliondollarextreme.tv" ||
window.location.href == "http://www.milliondollarextreme.tv/") {
window.location.href = "http://www.milliondollarextreme.tv/tagged/videos/";
}
</script>
What should happen when someone enters to http://milliondollarextreme.tv/ for the second time?

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>

JS redirect according to user agent?

I'm sorry if that question is obvious to solve, but I don't know JavaScript yet but feels like it's the only way to implement what my clients need for their website.
I want to redirect users with specific browser to the specific simplified page once they visit homepage and keep this redirection work everytime they click on homepage button. Basically what I want is a 301 redirect based on user-agent. I've tried many times but all I've got was an infinte redirection loop or no result at all.
function get_ya_browser(){
var ua = navigator.userAgent;
if (ua.search(/YaBrowser/) > 0) document.location.href='redirect-link.html';
return '';
}
How could I change this code to work? Would really appreciate any help.
Try - document.location.replace ='redirect-link.html';
instead of document.location.href ='redirect-link.html';
.location.replace() simulates a HTTP redirect
.location.href() simulates a mouseclick
If the code was part of redirect-link.html && ua.search(/YaBrowser/) !== -1, you will get an infinite loop.
following code solved this problem:
function get_ya_browser(){
var ua = navigator.userAgent;
if (ua.search(/YaBrowser/) !== -1 && !location.href.includes('elbrus-m-zakaz.ru/home-page-windows-xp/')) {
document.location.href='//elbrus-m-zakaz.ru/home-page-windows-xp/';
}
}

Redirect user based on useragent

I don't know JavaScript at all. However, I've made an Android app in which I'm using a WebView to load a webpage. I have a Share option in the menu that allows to share the URL that's then loaded in the WebView.
For a particular page, I would like the users who are not using the app to be redirected to a different page. To make this possible, my Android app is using a custom user agent (say CustomUA, in this case). So, whenever my app loads this particular webpage (say page1.html), I want it to load normally inside the app. However, suppose the URL is shared, I would like the URL to be redirected to say page2.html if it's visited using any browser. Basically, I want only the UA of my app to be able to access page1.html and every other UA is to be redirected to page2.html.
I suppose this can be done using JavaScript as I read some other solutions. I couldn't implement those as they were not exactly my case and I'm not sure how to implement them. I suppose, I just have to place that script in my page's body?
Also, just in case, if I'm supposed to include multiple user agents in the whitelist (i.e., if I want CustomUA and CustomUA 2, both to be able to access the webpage), how do I modify the code?
EDIT:
I have found this code to be included in <head>:
<script>
if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0))
{
window.location.replace("your page");
}
else
{
window.location.replace("your page");
}
</script>
Now, can someone lease help me to modify it according to my needs? I just want the webpage to be redirected if it's not my custom User Agent and to stay on the same page if it is.
EDIT:
I have used the code like this:
<script>
if ((navigator.userAgent.indexOf('customUA') < 0)
{
window.location.replace("page2.html");
}
</script>
however, I'm getting Uncaught SyntaxError: Unexpected token { eror in console and the redirect isn't working.
EDIT:
Got around the Uncaught error. New code:
<script type="text/javascript">
if (navigator.userAgent.indexOf('customUA') >= 0)
{
window.location.replace("page2.html");
}
</script>
Now, it's working fine in either of the one i.e., it'll redirect where I don't want it to and vice versa. I've tried multiple expressions like < 0, > 0, == -1, etc. None worked fine. Basically, it's not giving the expected results. I'm this close to find my answer.. please, help!
Some guys from a forum helped me, also, I was setting the wrong user agent (the one that I had defined in my app was different than what I had typed in this script). So, here goes the code:
<script type="text/javascript">
var uaCheck = navigator.userAgent.indexOf('Custom User Agent');
if (uaCheck == -1)
{
window.location.replace("https://www.domain.tld/redirected-page.html");
}
</script>
And this is the code if there are multiple user agents:
<script type="text/javascript">
function isMyAppUA(testUA)
{
if(testUA == undefined) return false;
var appUAList =
[
'customUA1', 'customUA2', 'customUA3', ..., 'customUAn'
];
appUAList.forEach((ua) =>
{
if(testUA.indexOf(ua) > -1)
{
return true;
}
});
return false;
}
if(!isMyAppUA(navigator.userAgent))
{
window.location.replace("https://www.domain.tld/redirected-page.html");
}
</script>
There's a shorter version of this code if the list of user agents is small:
<script type="text/javascript">
if (navigator.userAgent.indexOf('customUA') == -1 || navigator.userAgent.indexOf('customUA2') == -1)
{
window.location.replace("https://www.domain.tld/redirected-page.html");
}
</script>
Any of these codes go in the <head> of the HTML page.
First off, unless there is good technical reason to want to do this, don't. This is the internet; let people access your pages however they wish.
Second, it might be easier to do this the other way around- have a set of normal pages on the web, but if they are accessed in the app, the app detects them, and navigates to an app-specific version.
Finally, if you do do this, for easily handling multiple user agents, you would want to make an array containing all the user agent strings you want, and loop through them to check each one. Follow your favorite JavaScript tutorial on arrays, loops, etc.

Am I using window.location.replace right? (also am I writing commands right?)

I kind of get all my coding information from the internet rather than a class, and certain answers I can't really understand so hopefully I'll get a simple answer to a possibly stupid question.
In a part of my website, I've got a button which brings up a window.prompt and it asks the viewer if they would like to go to one page (I've called it Timeline because it's a timeline of my art portfolio) or to the other. The prompt works perfectly fine, and I attempted to code it so that if, for example, the user typed in "Timeline" the webpage would automatically redirect them to the Timeline page (which for me is empty at the moment) and vice versa if they typed in "Term 1" (the name of the other page.) I found out about window.location.replace and assumed that it would work if I tried to get it to redirect the user, but when I run my webpage nothing happens after I type in the prompt window. I checked the Developer Tools on Chrome and it didn't report any errors, so is it just me typing some stuff wrong?
function promptFunction() {
var choice = window.prompt ("Would you like to go to the Timeline or to Term 1?")
if (choice === "Timeline")
function redirTime() {
window.location.replace() ("Timeline.html");
}
else if (choice ==="timeline")
function redirTime() {
window.location.replace() ("Timeline.html");
}
else if (choice ==="Term 1")
function redirFirstTerm() {
window.location.replace() ("Term 1.html")
}
else if (choice ==="term 1")
function redirFirstTerm() {
window.location.replace() ("Term 1.html")
}
};
This again is probably me looking for an answer that's right under my nose, so if the answer is something obvious, sorry ^^'
Edit: I've got an answer now; thank you very much! (I did think it was something to do with the way I was typing it, sorry)
The code you posted creates, based on choice a function, but the function is never executed (or you omitted this code).
Additionally the window.location.replace()("...") part is wrong. It calls the replace function on window.location with no arguments and uses the result of this function call again as function an passes e.g. "Timeline.html" as argument.
If all you want to do is setting the location, assign it directly, e.g. window.location = "https://stackoverflow.com" or in your case window.location.href = "Timeline.html" (see https://developer.mozilla.org/en-US/docs/Web/API/Window/location).

Will this hash code change the URL location on randomize?

still practicing javascript.
Here is a JSBIN of a site that I have made that I can't seem to figure out how to make it so the URL will change once the video is clicked or randomized.
(page load/refresh and click both trigger this)
http://jsbin.com/vesebu/1/edit?html,js,output
The page randomizes on load and page click with an array of videos.
What I am trying to figure out is how to change the URL by appending an ID to the end of it. ie: mysite.com/6530 <-- last four digits representing the ID of the video. This will make the site shareable and is what I am trying to figure out how people are able to do this!
I have tried reading documentation on history.js, html5 pushstate as well as window.location hash and nothing seems to work.
M = function(d) {
function a() {
var a;
a = window.location.hash;
/^#\d+$/.test(a) && (window.location.href = "/" + a.slice(1));
/^#!\d+$/.test(a) && (window.location.href = "/" + a.slice(2));
f
}
y(a, d);
return a
}(n);
I was suggested this code by a friend but can't seem to make sense of it, especially with the jsbin I have. I'm completely lost here on how to do this, and have really tried reading up on it.
any help at all is definitely appreciated!
Thanks guys
Your hashes may be like this:
#/2134/23423/
#2134/23423/
result:
windows.location = /2134/23423/;
then do something else.......
Hope it can help you ~

Categories

Resources