Javascript function will not execute using Cookies - javascript

I made a javascript function and I am trying to execute it in the javascript area. However, it will not run. When I run the code it just does nothing. I am using Github, so if you want my full code, go to https://github.com/TheNumnut/Wars-of-Shares/blob/master/Login/index.html
I am trying to make a Login page for a game. I would rather use forms than prompts but I have not been able to do that. If somebody could tell me how to use forms I will greatly appreciate it.
This is my code:
function checkCookie(checkusername, checkpassword) {
if (getCookie("username") != "") {
setCookie("username", checkusername, 365);
setCookie("password", checkpassword, 365);
}
else {
if (checkusername != getCookie("username") {
alert("Username wrong");
}
else {
if (checkpassword != getCookie("password") {
alert("Password wrong");
}
}
}
window.open("https://thenumnut.github.io/Wars-of-Shares/", "_self");
}
checkCookie(prompt("Username"), prompt("Password: ");
Please have a look at my other code in Github, because it also has not been working. Especially the profile page. None of the clickable text and links have been working. The link to the Profile Page is https://github.com/TheNumnut/Wars-of-Shares/blob/master/Profile/index.html

Your JS code has syntax errors.
You're missing an extra ) in the 7th, 11th and 42th line.
You can press F12 on your browser and check the console tab, where javascript errors are displayed.
However, I need to agree with the other users that this login method is not a good practice and it's very easy to bypass.

JavaScript source is available to the user of any website. Since the JavaScript runs in the user's browser, the code has to be transferred to the user's browser so the browser knows what to do. This means I as the user can view the source. In this case, by viewing the source I can see the line:
window.open("https://thenumnut.github.io/Wars-of-Shares/", "_self");
So I can bypass your log in page just by looking at the page source and going straight to https://thenumnut.github.io/Wars-of-Shares/

Related

Shopify page keep refreshing after being redirected by javascript code

I am trying to create a website https://fone-kase-plus.myshopify.com/ that will detect what device model it's being accessed from (for example GALAXY A40) so it will immediately redirect the user to a corresponding page.
It seems to be working, but it will reload the page multiple times after redirecting. If I try to redirect it to non-shopify page (eg stackoverflow.com) this problem doesn't occur.
function deviceAPIcallback(result){
if(result.deviceName == "desktop"){
window.location.href = "https://fone-kase-plus.myshopify.com/pages/galaxy-a40";
}
else{
alert(result.deviceName);
}
}
Can you please tell me what the problem can be or at least how I can detect it by dev tools?
Thanks
When I go to the site from both my macOS laptop and my iPhone I only get the alert aspect of your function. What does result.deviceName return? You might need to add a third '=' to your first 'if' statement.
function deviceAPIcallback(result) {
if(result.deviceName === "desktop") {
window.location.href = 'https...';
} else {
alert(result.deviceName);
}
}

JavaScript beginner needs help if/then/else

I need a javascript that reads the QR code, if the URL or instruction shown matches the URL we use, it executes that script
If not, it pops up a warning that the code link is not one of ours, and offers a button to let the user launch the QR link.
Yes, this is a mobile app for my company to check inventory
Here's what I have
if (data.text == 'http://gmail.com') {
window.open(data.text, '_system', 'location=yes');
}
else
{
window.location = 'http://www.jths.co.uk/index.phtml?d=541300';
}
the only part that works (if I remove the rest of it) is this
window.open(data.text, '_system', 'location=yes');
It could be that "data" is undefined or not an object as Sergeon mentioned.
To investigate the issue, you could open browser's Developer Tools, check the console to see if it had any exceptions. You could also put the breakpoint to the line of:
if (data.text == 'http://gmail.com') {
Then you could watch the value of data and data.text or step over.

Why does Javascript creates cookie after php function starts to run even if the one is first in order

I have a question about an order in code. I wanted to create a cookie with a language, and then use the function l to translate. This is the code:
<script>
var lang = navigator.language;
if (document.cookie.indexOf("LANG=")==-1) {
if (lang=="pl"||lang=="en-US") {
document.cookie = "LANG="+lang+";domain=xxx;path=/";
} else {
document.cookie = "LANG=en-US;domain=xxx;path=/";
}
}
</script>
<?php
include 'translations.php';
function l($t) {
global $trsl;
$l = $_COOKIE['LANG'];
if ($l=="en-US") {
return $t;
} else {
return $trsl[$l][$t];
}
}
?>
The problem that I find when I enter the website is that when you enter for the first time, it shows me an error saying that the requested cookie is not defined. But when I press Ctrl+F5 everything goes back to normal. I guess that the php code loads before the js code, but why is that? Is there some way to prevent it?
When you mean enter the website, you mean load the script for the first time?
You are confused about where each script is processed. Javascript as you write it is mean to load up and processed on the browser, while the php will be run on the host machine. So you php will be triggered and running before the whole thing is loaded up in the browser. hence the error.
If you are confused and want to know how it happen, trigger up chrome dev tools and swith to tab network. Then open the page and see header part of the request (make sure its show all / html). Then push f5 and compare from before. You will see that cookie header is assigned on the second request.
Hope it answer your question.

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).

HTML5 & Javascript

I know nothing about javascript. Just want to tweak a chunk of code a little bit, but couldn't find answers on the net, or at least one that i can understand. Hope you can help me out.
It's an onMouseover/onClick javascript (plays audio) event for HTML5. The HEAD part of the script says in one place:
<Script>
............
...........
}
else{
return {playclip:function(){throw new Error("Your browser doesn't support HTML5 audio unfortunately")}}
}
In other words, as best as I can understand, this means if the User's browser doesn't support HTML5 audio, the script will return this error message/dialog: "Your browser doesn't support HTML5 audio unfortunately".
But, I don't want this error message/dialog to be returned/generated/thrown whatever! Instead, I would prefer greatly the script to send the User automatically, before it shows any error message whatsoever, to a NEW non-HTML5 javascriptless plain webpage.
What can I do?
Your else statement returns an object with one property (called playclip) which is a function that throws an error. If you'd like to redirect the user to another page instead, simply do a redirect instead of throwing an error:
else {
return {
playclip: function() {
// it might be nice to tell a user why they're being redirected:
alert('You are being redirected to another page because your browser does not support HTML5 audio!');
window.location.href = 'whateverPageYouWant.html';
}
};
}
you could do
window.location.href = "myhtml.html";
to direct to a new site immediately instead of the line "return..."
the simplest thing to do is change the location to another url.
else {
window.location = 'http://newnonhtml5webpage/';
}

Categories

Resources