`window.location.href` not working as intended - javascript

I'm using a JavaScript, along with PHP, to create a login system for a project I'm working on.
I have everything working in regards to actually logging the user in, and then checking this within my JavaScript. However when I redirect the user from the login page to a private page, I'm having issues with window.location.href in the JavaScript.
I own the domain thomas-smyth.co.uk, so I have tried the following piece of code to redirect the user.
window.location.href = "http://thomas-smyth.co.uk/home.php";
However, when this redirects me, it is redirecting me to thomas.smyth.co.uk, which obviously causes the page not to load. Any ideas on how I could fix this?
Thanks in advance,
Tom

You have posted the wrong code. In your Js File thomas-smyth.co.uk/functions/login.js on your webserver is this:
//Re-directs to subdomain.
window.location.href = "http://www.thomas.smyth.co.uk/home.php";
this should be fixed to
//Re-directs to subdomain.
window.location.href = "http://thomas-smyth.co.uk/home.php";
#vlaz mentioned right, you have a second mistake with a wrong dot instead of a dash
And btw you should explain your problem a bit more. More code would be usefull next time if you want some help.

Related

Automatic random URL redirect

So I have a website on Google Sites, and I want to add a 'random post/page' button. I found this code on GitHub and edited it by putting my own URL's. Everything works according to plan except the code uses cookies, basically you go to the link once, it redirects you to a random URL that you have chosen, and it saves that URL. I want to make it so that it's always random, no cookies. I've tried altering the code but nothing seems to work since I don't know much about code, except for the basics. Could anyone help remove the use of cookies of this code? You can view the code below. Thank you.
This is the GitHub code
I tried deleting any code that I thought could be related to the cookies, basically the bottom of the code. The code still worked, but the cookies we're still saving. Like I said, I only know the basics so I was just deleting and praying it would fix it.
Can be massively simplified if you don't need the cookie functionality:
const redirectUrls = [
"http://www.aftonbladet.se/",
"http://www.dn.se/",
"http://www.svd.se/",
]
document.location = redirectUrls[Math.floor(Math.random() * redirectUrls.length)]

How can I make a Java Script to redirect to another domain?

my name is Angel and I am not a programmer nor do I have previous experience with any language, today I have noticed that a forum to which I belong bought a new domain, but there is a problem with cookies, since it is not possible to log in if accessed from the new domain.
Many users have had this problem and I wanted to try to make a java script to solve this problem, but the furthest I got was that any web page was redirected to the forum home page, reloading over and over again.
I'm not good at programming, but Javascript has piqued my interest, because of the ease of transforming it into a browser extension.
What I mean is redirect from:
forum.free.com/discussion/8873
to
cooldomain.com/discussion/8873
and that every url with the domain "forum.free.com" redirects to the same page, only with the new domain of "cooldomain.com"
I know that it is a simple script and that it is too much to ask, but the intention is to learn through the use of annotations the reason for each line and to help the users of said forum.
Thank you.
This is the "script" i "made":
switch(window.location.hostname){
case "forum.free.com":
window.location.replace("cooldomain.com");
break;
...
}
you can try something like this:
<script language=javascript>
function redirect(){
window.location = "http://cooldomain.com/discussion/8873";
}
</script>

URL address changing from localhost:3000/home to localhost:3000/home#_=_

I configured my project first with passport-google-oauth20 authentication and it was working fine.
I added the passport-facebook after that and its working fine as well, however, after logging/registering with my FB account my redirecting URI which is supposed to be "http://localhost:3000/home" shows "http://localhost:3000/home#=" instead. It isn't showing any errors in the console and also working as it is intended to. I've got no clue what part of code I'm suppose to share with you guys as the functionality of the project is intact, so please let me know the snippet which may help you to better understand the issue.
This is not a problem itself.
https://github.com/jaredhanson/passport-facebook#why-is-__-appended-to-the-redirect-uri

Alert box after redirect

so i know this may sound stupid, but I want to know if there is any way that i could redirect someone to a website, and then display an alert box on the web page, using either javascript or any other interface that can be weaved into HTML5. I asked some of my classmates, and they didn't know, so I just need a confirmation that this isn't possible.
I have ran a few trials i found, but on further review, they wouldn't work.
edit I have control over the site, but I wish for the box to only pop up if i redirect it.
I can give the code if it would help, but I'm doubtful it will help
sorry for wasting your time if i did. thanks.
Similar to Gerard's answer, but using query strings. A possibly more standard solution.
On the first page:
<script>
window.location = 'https://{your website here}/?showAlert=true';
</script>
Then on the 2nd page.
const urlParams = new URLSearchParams(window.location.search);
const showAlert = urlParams.get('showAlert');
if(showAlert === "true") {
alert("Hello");
}
Note this will not work in internet explorer
You could communicate to the new site that it's a redirect by appending the route:
On the site you want to redirect from:
<script>
// if you can't use a normal link, change the url with JS
window.location = 'https://{your website here}/#redirect';
</script>
On the site you want to show the alert:
<script>
if (window.location.pathname.includes('#redirect') alert('What you want to say');
</script>
If you only have control over the first site, you could alert before redirecting, there wouldn't be much difference to the user since the alert blocks execution of other code.
You can call an alert box in a HTML document with JavaScript like this:
window.onload = function () {
alert("Hello World!");
}
-> https://jsfiddle.net/u8x6s31v/
You can also redirect somebody, if the page he's visiting is yours. Is that what you mean?
Update:
I think you can't see the URL where someone comes from with JavaScript. The only way to trigger scripts for some people is to add a hash.
window.onload = function () {
hashUrl = window.location.hash;
if (hashUrl == "#alert") {
alert("Hello World!");
}
}
Then call for example: domain.tld/path/index.html#alert
-> https://jsfiddle.net/u8x6s31v/1/
This is a vague questions with a lot of possible answers but seeing as how you're new, I'm going to speculate on what you might be asking and try to answer it.
It's not clear what you mean by "redirect".
Is this a server-side redirect like when you move/change a URL and you redirect the old URL to a new URL? Is this a Javascript meta refresh that you put in a 404 template? Is this a redirect that occurs after a user takes an action?
Regardless, you're going to have to "annotate" that user and then take action upon that annotation. The most obvious method would be based on the "referrer" HTTP header but it is also possible to do it based on the presence of a cookie.
Additionally, adding URL parameters to a redirect is trivially easy and often used for stuff like this.
The immediate things that come to my mind would be via Google Analytics (preferably implemented via Google Tag Manager because it'll make it easier).
Look into "outbound link tracking", "cross-domain tracking" and "UTM campaign tracking" (all related to Google Analytics and/or Google Tag Manager) and you'll probably find something that suits your needs.
URL shorteners are commonly used to mask parameters in links and there are open source libraries that allow you to host your own URL shortener (which you could integrate into your redirects) or do some other type of link tracking like is often used for affiliates.

I want a codes which redirect to my website when JavaScript used on wrong site

I want to secure JavaScript Codes. I secure my JavaScript with https://javascriptobfuscator.com/ but it can also stolen from view source.
I want a codes which redirect to my website when JavaScript used on wrong site.
This is possible I know but I don't know how?
What you could do is check the return of location.host or location.hostname.
To redirect to another URL you would change the value of location:
location = "https://mywebsite.com";
//or alternatively
location.assign("https://mywebsite.com");
However, the redirection may will be prevented by Cross-origin resource sharing policies (see Michael's comment). So you could just display an alert dialog.
alert("Don't use my script on your website");
The person trying to use your code on their website will be warned during development by this alert.
In any case, your obfuscated code can still be changed and someone that is determined will remove these "security" measures and it can even be deobfuscated.
Are you sure all of this is needed ? Modifying a stolen code to make it work on another website can be harder/longer than just rewriting a similar code. Apart if you are doing something very special and new, I think you are just losing time by trying to protect your code.

Categories

Resources