How to show dialogue if url contains something - javascript

If my URL is any of these type:
domain.com/page.html?refer=whatsapp
domain.com/page.html#whatsapp
Then I want to show a dialogue that shows "You have been referred from Whatsapp".
And if the url is accessed without the whatsapp part, then it must not show the dialoge box.
What I have tried is:
<script type="text/javascript">
var url = window.location.href;
if (url.search("#wa") >= 0) {
//found it, now do something
}
alert("Good Afternoon");
} else {
null //if not just do nothing
}
</script>
But no luck.

It helps you
var url = window.location.href;
// For example. In real app remove it
url = 'http://website.com/page.html#whatsapp';
// Search our #ID
var source = url.substring(url.lastIndexOf('#') + 1);
// For salutation text
var salutation = null;
// Iterate variants
switch ( source ) {
case 'whatsapp' : salutation = 'You have been referred from Whatsapp'; break;
default : salutation = 'unknown website';
}
alert ( salutation );

Related

Inputting different passwords always redirects to the HTML page of the first password

I am trying to make a password system, in which for specific password, it redirects to a custom web-page, Like a support-type system
But whenever I type the password: tanishq1e01, or tanishq1e02, it always returns the webpage for
tanishq1e02 webpage. Why?
I expected that it would output a different webpage for each password, but it doesn't work!
var ff;
let tanishq1e01 = "tanishq1e01";
let tanishq1e02 = "tanishq1e02"
ff = prompt("Enter Given Password By Prattay: ");
if (ff == tanishq1e01) {
window.location.href = "suppstsasd/tanishq1e01suppasd.html";
} else if (ff = tanishq1e02) {
window.location.href = "suppstsasd/tanishq1e02suppasd.html";
} else {
confirm("Not Right Password! Refreshing...");
window.location.reload();
}
You have a = assignment instead of == or === compare in the second test.
Here is a more elegant way, where it is easier to add locations
This is assuming you want to use such insecure way to load pages.
const locations = {
"tanishq1e01": "suppstsasd/tanishq1e01suppasd.html",
"tanishq1e02": "suppstsasd/tanishq1e02suppasd.html"
}
let ff = prompt("Enter Given Password By Prattay: ");
let loc = locations[ff];
if (!loc) {
alert("Not Right Password! Refreshing...");
window.location.reload();
}
else {
console.log("Going to", loc);
window.location.href = loc;
}

My input in the form gets reset when I hit submit . I also tried using console.log() to test if my code is correct but it nothing shows in console

I am creating a form that asks for email and password and validates it. If it has any error, it pops on the same line. Below is my javascript code for it. When I fill the form and hit submit, the values get reset. Moreover, I do not see anything in the console. I tried using a random code console.log('Hanee') to test my code but nothing gets generated in the console tab. Could anyone tell me what's the issue here?
Also, here's the link to my login form: http://www2.cs.uregina.ca/~hsb833/215a/login.html
document.getElementById("login-form").addEventListener("submit",validateLogin,false);
function validateLogin(event){
var elements = event.currentTarget;
var emailValue = elements[0].value;
var passValue = elements[1].value;
checkEmail(emailValue);
checkPass(passValue);
}
function checkEmail(emailValue){
var regex_email = /^[/w]+#[a-zA-Z]+?\.[a-zA-Z]{2,3}$/;
var index = emailValue.search(regex_email);
var errorEmail = document.getElementById("errorEmail");
var valid = true;
console.log('Hanee');
if(index != 0){
errorEmail.style.display = 'inline';
}
}
function checkPass(passValue){
var password = document.getElementById("password");
var regex_pass = /[\w]+\S/;
var index = passValue.search(regex_pass);
if(passValue.length < 6){
console.log('password is invalid. Minimum length is 6');
errorPassLen.style.display = 'inline';
}
if(index != 0){
console.log('password is invalid. Please make sure there is no spaces in between');
errorPassFormat.style.display = 'inline';
}
}
form is refreshed after being submitted by default. To prevent that, use event.preventDefault(); in your validateLogin
Here is an example of a form with a username and password, maybe it will suit you and you can customize it for your needs
async function tebeFxhr() {
let logfil = 1;
let sotebe= false;
let tes =(logt) => {
return /^[a-zA-Z0-9]+$/ui.test(logt);
}
tes(gtebe)==true ? logfil++ : alert('Login cannot contain characters or spaces')
let textrf =(rutext) => {
return /[а-я]/i.test(rutext);
}
textrf(stebe)==true ? alert('The password cannot contain Cyrillic characters') : logfil++;
stebe.length < 8 ? alert('Password is very short') : logfil++;
stebe===ftebe ? logfil++ : alert('Enter the password 2 times so that they match')
if (logfil === 5){
const data = {data_0:gtebe, data_1:stebe, data_2:ftebe};
const response = await fetch("/form", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then((response) => {
return response.text();
})
.then((sotebe) => {
console.log(sotebe)
if(sotebe==='error-1'){
console.log('error-1')
}
else{
sotebe =='' ? location.replace("../") : alert(sotebe)
}
});
}
}
When submit event is triggered, all values are sent to server and get reset. If you want to prevent this, use event.preventDefault(); inside the event handling function validateLogin(event). Then, the values are not going to be reset!
Still If you want your values to be empty after submission, try below.
elements.forEach(e => e.value = '');

How to change page with JS in html?

I want when the user successfully sign up it goes to the main page.
I already tried a lot of function but it doesn't work..
code :
var nama = document.getElementById("name");
var email = document.getElementById("email");
var userPw = document.getElementById("pass");
sign up code :
function store(){
var rePass = document.getElementById("re_pass");
if (userPw.value == rePass.value && nama.value != "") {
alert("Berhasil daftar!");
} else {
alert('Coba periksa lagi~');
return;
}
localStorage.setItem('namaUser', nama.value);
localStorage.setItem('emailUser', email.value);
localStorage.setItem('passwordUser', userPw.value);
i tried this function also :
self.location = 'sign in.html';
}
Try this one. make sure to clear the space between in this segment!!! 'sign in.html'
window.location = 'sign in.html'
Try this.
window.location.replace("/");
or
window.location.href = "/";

i want show alert with page url

I am trying to show alert message when user failed to login with url value .
<script type="text/javascript">
<!--
var retVal = confirm("Login FAILED! Do you want to continue ?");
if( retVal == true ){
window.location.href = "login.php";
return true;
}else{
alert("User does not want to continue!");
return false;
}
//-->
</script>
this is url value
/?redirect_to=%2Fstarstruckmedia%2F%3Flogin%3Dfailed%26redirect_to%3D%252Fstarstruckmedia%252F&login=failed
You can't use return unless you are in a function. Take those lines out and your code works.
var retVal = confirm("Login FAILED! Do you want to continue ?");
if(retVal){
window.location.href = "login.php";
} else {
alert("User does not want to continue!\n" + window.location.href);
}
You can obtain the url of the current page using window.location.href. See this for details.
var retVal = confirm("Login FAILED! Do you want to continue ?");
if( retVal == true ){
window.location.href = "login.php";
}else{
alert(window.location.href);
}

Dual language browsers redirect Javascript

I am trying to redirect English browsers to xyz.com/?lang=en while letting Swedish ones stay on xyz.com
I have been trying :
var type=navigator.appName
if (type=="Netscape")
var lang = navigator.language
else
var lang = navigator.userLanguage
//cut down to first 2 chars of country code
var lang = lang.substr(0,2)
// Swedish
if (lang == "sv")
window.location.replace('????')
// if none of above (default to English or any other)
else
window.location.replace('xyz.com/?lang=en')
</script>
But I don't know how to write the Swedish URL since it's not a redirection as the default language is swedish... writing the xyz.com gets me into a redirection loop
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langlight">Svenska</div>';
}
else
{
echo '<div class="langbold">Svenska</div>';
}
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langbold">English</div>';
}
else
{
echo '<div class="langlight">English</div>';
}
enter code here
if (lang !== "sv") {
window.location.replace(window.location.href + '?lang=en');
}
You are always checking the language from the navigator.
This way everytime the page loads it will try to reload the page with the altered or not url.
You need to place a condition where it will not reload the page..
That should be when there is a URL parameter passed.
So check for the querystring as well and override the default if a value exists in the url.
<script type="text/javascript">
(function(undefined){
var queryString = {},
search = window.location.search.substring(1), // read the current querystring
searchItems = search.length?search.split('&'):[]; // and split it at the different params if any exist
for(var i = 0, len = searchItems.length; i < len; i++){ // for each parameter passed
var parts = searchItems[i].split('='); // split the key/value pair
queryString[parts[0].toLowerCase()] = parts[1]; // and store it to our queryString variable
}
if (queryString.lang === undefined){ // if there is no lang parameter passed do your checking otherwise skip this step completely and use the url lang parameter.
var type=navigator.appName, lang = '';
if (type=="Netscape") {
lang = navigator.language.substr(0,2);
} else {
lang = navigator.userLanguage.substr(0,2);
}
if (lang != "sv"){
if (searchItems.length){
window.location.replace(window.location.href + '&lang=en');
} else {
window.location.replace(window.location.href + '?lang=en');
}
}
}
}());
</script>
Notice: Although, as #missingno mentions in his comment to your question, this is best handled server-side than client-side.

Categories