Script stops working when I insert into another script - javascript

I have a problem that is killing me right now because I can't see why it isn't working. I have made a script in one file that works, but when I copy/paste it into the file that I want it to work in, it doesn't work.
Here's the script I just made:
<html>
<body>
<p id="myElement"></p>
<script>
if (window.location.hash == "#hello") {
document.getElementById("myElement").innerHTML = 'Vi har Hello';
} else {
document.getElementById("myElement").innerHTML = '';
}
</script>
</body>
</html>
Here it works, but when I insert it into the file that it was meant for it won't work. Here it is:
<html>
<head>
<title>...</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="layout/styles/layout.css" rel="stylesheet" type="text/css" media="all">
<link rel="shortcut icon" href="images/favicon.ico">
</head>
<body id="top">
<div id="forside"></div>
<div class="bgded overlay" style="background-image:url('images/stor_1.png');">
<div id="pageintro" class="hoc clear">
<li>
<p></p><h2>Log ind</h2>
<p id="myElement"></p>
<script>
if (window.location.hash == "#hello") {
document.getElementById("myElement").innerHTML = 'Vi har Hello';
} else {
document.getElementById("myElement").innerHTML = '';
}
</script>
<div id="comments">
<font color="black">
<form action="proces.php" method="post">
<input class="form-control" type="text" id="name" size="22" name="username" placeholder="Brugernavn" required><br>
<input class="form-control" type="password" id="name" size="22" name="pass" placeholder="Kodeord" required><br>
<input type="submit" name="submit" value="Log Ind!">
</form>
</font><br>
<i class="fa fa-chevron-left" aria-hidden="true"> Tilbage til forsiden</i>
</div>
</li>
</div>
</div>
<?php include_once "footer.php"; ?>
<a id="backtotop" href="#top"><i class="fa fa-chevron-up"></i></a>
<script src="layout/scripts/jquery.min.js"></script>
<script src="layout/scripts/jquery.mobilemenu.js"></script>
</body>
</html>
What the script, I am inserting, does is to detect if #Hello is in the URL and if it is, it has to display some text... But it won't work.
Anybody who has an idea about what I could be doing wrong?

your code will only run once when your page document first time load, if you do want to check the hash every time its changed, it is better to put in a change callback.
please also be sure you jQuery relative path is right.
$(function() { // ensure you will execute script after document loaded
$(window).on('hashchange', function() { // put you code in side change callback.
if (window.location.hash == "#hello") {
document.getElementById("myElement").innerHTML = 'Vi har Hello';
} else {
document.getElementById("myElement").innerHTML = '';
}
});
});
<html>
<head>
<title>...</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="layout/styles/layout.css" rel="stylesheet" type="text/css" media="all">
<link rel="shortcut icon" href="images/favicon.ico">
</head>
<body id="top">
<div id="forside"></div>
<div class="bgded overlay" style="background-image:url('images/stor_1.png');">
<div id="pageintro" class="hoc clear">
<li>
<p></p>
<h2>Log ind</h2>
<p id="myElement"></p>
<script>
</script>
<div id="comments">
<font color="black">
<form action="proces.php" method="post">
<input class="form-control" type="text" id="name" size="22" name="username" placeholder="Brugernavn" required>
<br>
<input class="form-control" type="password" id="name" size="22" name="pass" placeholder="Kodeord" required>
<br>
<input type="submit" name="submit" value="Log Ind!">
</form>
</font>
<br>
<i class="fa fa-chevron-left" aria-hidden="true"> Tilbage til forsiden</i>
</div>
</li>
</div>
</div>
<a id="backtotop" href="#top"><i class="fa fa-chevron-up"></i></a>
<script src="layout/scripts/jquery.min.js"></script>
<script src="layout/scripts/jquery.mobilemenu.js"></script>
<script type="text/javascript">
$(function() {
$(window).on('hashchange', function() {
if (window.location.hash == "#hello") {
document.getElementById("myElement").innerHTML = 'Vi har Hello';
} else {
document.getElementById("myElement").innerHTML = '';
}
});
});
</script>
</body>
</html>

try adding these
<script src="layout/scripts/jquery.min.js"></script>
<script src="layout/scripts/jquery.mobilemenu.js"></script>
at the top in the head section

Related

input validation does not work - form gets submitted anyway?

I'm creating a website with a form and I can't get the validation to work, and can't figure out what I'm doing wrong. I've tried everything I can think of. The PHP works and it will submit, but it will always submit, and won't stop itself from submitting.
<!DOCTYPE html>
<html lang="En">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="a website for carley Knight art">
<meta name="keywords" content="art artist painter painting store fine-art">
<link rel="favicon icon" href="Images/favicon.ico">
<title>Carley Knight Art</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="slicknav.css">
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.jsdelivr.net/npm/slicknav#1.0.8/dist/jquery.slicknav.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation#1.19.3/dist/jquery.validate.min.js"></script>
<script src="js/final.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#nav_menu').slicknav({
prependTo: "#mobile_menu"
});
});
</script>
</head>
<body>
<header>
<img src="Images/Pink%20Stationery%20Literacy%20Google%20Classroom%20Header.png" alt="header">
</header>
<Nav id="mobile_menu"></Nav>
<nav id="nav_menu">
<ul>
<li><a class="current" href="Index.html">Home</a></li>
<li>About</li>
<li>Gallery</li>
<li>Blog</li>
<li>Shop</li>
<li>FAQs</li>
</ul>
</nav>
<main>
<section>
<div id="top">
<h1>Homepage</h1>
<p>Welcome to Carley Knight Art!</p>
</div>
<img src="Images/IMG_2995-768x1024.jpg" alt="carley knight and art" class="center" id="carley">
<p>Hello My name is Carley Knight and I'm an artist living in Milwaukee, WI. I make abstract art in a variety of different mediums, including fibers, acrylics, and digital.</p><br>
<button id="readMore">Read More</button>
</section>
<aside>
<form action="sendmail.php" method="post" name="contact_form" id="contact_form">
<fieldset>
<legend>sign up now!</legend><br>
<p>Sign up for my email list and get a free mini coloring book!</p><br>
<img src="Images/minicoloirngbook.jpg" alt="mini coloring book"><br>
<label for="first_name">First Name:</label>
<input type="text" name="first_name" id="first_name"><span>*</span><br>
<label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="last_name"><span>*</span><br>
<label for="email">Email Address:</label>
<input type="email" name="email" id="email"><span>*</span><br>
<label for="verify">Verify Email:</label>
<input type="email" name="verify" id="verify"> <span>*</span><br>
<div id="buttons">
<input type="submit" id="submit" value="Sign Up">
</div>
</fieldset>
<script>
$("#contact_form").validate();
debug: true
</script>
</form>
</aside>
</main>
<footer>©copywrite 2021 Carley Knight Designs</footer>
</body>
</html>
In HTML all you have to do is give element input the attribute required.
If you wish for more advanced validation then you have to do it in javascript.
<form>
<input type="text" required>
<input value="GO" type="submit">
</form>

How to use jquery to go to the next page or window in my Electron app

I'm trying to create a login system. I want the system to take me to another page "page3.htm" when I press the login button. I have been trying to test my system in jquery, but the code below does not seem to work. Why is that?
The "login.js" file contains:
window.onload =function() {
var loginBtn = document.getElementById('go');
loginBtn.addEventListener('click',function(){
document.location.href = './page3.html';
});
}
The "index.html" file contains:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Insert this line above script imports -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<!--Jquery -->
<script>window.$ = window.jQuery = require('./jquery-3.3.1.min.js');</script>
<!--Jquery end -->
<!--login css -->
<link rel="stylesheet" type="text/css" href="/bootstrap/css/login.css">
<!--login css ends here -->
<!--Database link -->
<script src='dbcon.js'></script>
<!--database link ends -->
<!--login link -->
<script src='login.js'></script>
<!--login link ends -->
<style>
</style>
</head>
<body>
<form>
<h1>Fashion World</h1>
<div class="inset">
<p>
<label for="username">Username</label>
<input type="text" name="username" id="username">
</p>
<p>
<label for="password">PASSWORD</label>
<input type="password" name="password" id="password">
</p>
<p>
<input type="checkbox" name="remember" id="remember">
<label for="remember">Remember me for 14 days</label>
</p>
</div>
<p class="p-container">
<span>Forgot password ?</span>
<input type="submit" name="go" id="go" value="Log in">
</p>
</form>
</body>
</html>

Javascript form validation error like facebook

I am trying to create a login page like facebook. Used html and javascript I managed to create login page with predefined username and password.If you type correct password then it will redirect to a new page. But if you type wrong password or username then it will show error message on the side of textbox like real facebook
my code is
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fakebook -Login</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body class="login">
<div id="facebook-Bar">
<div id="facebook-Frame">
<div id="logo"> Facebook </div>
<br>
<br>
<br>
<br>
<div class="loginbox radius">
<h2 style="color:#141823; text-align:center;">Log in to Facebook</h2>
<div class="loginboxinner radius">
<div class="loginheader">
</div>
<div class="loginform">
<form id="login" action="" method="post">
<p>
<input type="text" id="username" name="username" placeholder="Your Email" value="" class="radius" />
</p>
<p>
<input type="password" id="password" name="password" placeholder="Password" class="radius" />
</p>
<p>
<button class="radius title" onclick="check(this.form)" name="client_login">Log In</button>
</p>
<script language="javascript">
function check(form)
{
if(form.username.value == "rohit" && form.password.value == "password")
{
window.open('target.html')
}
else
{
document.getElementById("username").innerHTML = "wrong username"
}
}
</script>
<br>
<br>
forgot your password?</label></td>
| sign up for facebook</label></td>
</form>
</div>
</div>
</div>
</body>
</html>
Here is the facebook error message

getElementById().value gives blank for a text box

I am trying to show the value entered in a text box into a javascript alert(). I have a form on whose onSubmit(), I am calling a function checkLogin() in which I am alerting the value entered in the #test_user text box. Below is the HTML code:
<form id="login_form" name="login_form" method="post" action="verifylogin.php" onSubmit="checkLogin()">
<input type="text" id="test_user" name="username" class="inputbox" placeholder="Username" value=""/>
<input type="password" id="password" name="password" class="inputbox" placeholder="Password" />
<input type="submit" id="login" name="submit" value="LOG IN">
Login with facebook
</form>
And below is the checkLogin() function in js:
function checkLogin() {
var uname = document.getElementById('test_user').value;
alert(uname);
}
I need to show the text entered in the text box with id #test_user in the alert but the alert does not show anything. A blank alert is displayed.
I know this can also be done by passing the value as a parameter to the function but I wanted to know why this way isn't working.
Please help.
UPDATE: It does work when I use this code separately but when I am doing this in a website I am developing, this issue arises. I am not able to understand why this is happening.
UPDATE: Below is all my HTML code:
<!doctype html>
<!--[if IE 7 ]> <html lang="en-gb" class="isie ie7 oldie no-js"> <![endif]-->
<!--[if IE 8 ]> <html lang="en-gb" class="isie ie8 oldie no-js"> <![endif]-->
<!--[if IE 9 ]> <html lang="en-gb" class="isie ie9 no-js"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en-gb" class="no-js"> <!--<![endif]-->
<head>
<title><?php echo $title?> | App Contest</title>
<meta charset="utf-8">
<meta name="keywords" content="app contest" />
<meta name="description" content="Welcome to App Contest" />
<link rel="stylesheet" href="<?php echo base_url(); ?>css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="<?php echo base_url(); ?>addons/superfish_responsive/superfish.css" type="text/css" />
<link rel="stylesheet" href="<?php echo base_url(); ?>css/template.css" type="text/css" />
<link rel="stylesheet" href="<?php echo base_url(); ?>css/updates.css" type="text/css" />
<link rel="stylesheet" href="<?php echo base_url(); ?>css/custom.css" type="text/css" />
<!-- This stylesheet only adds some repairs on idevices -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="<?php echo base_url(); ?>css/responsive-devices.css" type="text/css" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Lato:300,400,700,900&v1&mp;subset=latin,latin-ext" type="text/css" media="screen" id="google_font" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400,400italic,700&v1&mp;subset=latin,latin-ext" type="text/css" media="screen" id="google_font_body" />
<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="<?php echo base_url(); ?>js/jquery-1.8.2.min.js">\x3C/script>')</script>
<script src="<?php echo base_url(); ?>js/jquery.noconflict.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>js/common_functions.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.1/modernizr.min.js" type="text/javascript"></script>
<link rel="shortcut icon" href="<?php echo base_url(); ?>images/favicons/favicon.png">
<link rel="apple-touch-icon" href="<?php echo base_url(); ?>images/favicons/apple-touch-icon-57x57.png">
<link rel="apple-touch-icon" sizes="72x72" href="<?php echo base_url(); ?>images/favicons/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="<?php echo base_url(); ?>images/favicons/apple-touch-icon-114x114.png">
<!--[if lte IE 9]>
<link rel="stylesheet" type="text/css" href="css/fixes.css" />
<![endif]-->
<!--[if lte IE 8]>
<script src="js/respond.js"></script>
<script type="text/javascript">
var $buoop = {vs:{i:8,f:6,o:10.6,s:4,n:9}}
$buoop.ol = window.onload;
window.onload=function(){
try {if ($buoop.ol) $buoop.ol();}catch (e) {}
var e = document.createElement("script");
e.setAttribute("type", "text/javascript");
e.setAttribute("src", "http://browser-update.org/update.js");
document.body.appendChild(e);
}
</script>
<![endif]-->
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Facebook OpenGraph Tags - Replace with your own -->
<meta property="og:title" content="KALLYAS Template HTML"/>
<meta property="og:type" content="website"/>
<meta property="og:url" content="http://www.hogash.com/demo/kalypso_html/"/>
<meta property="og:image" content="http://www.hogash.com/demo/kalypso_html/images/logo.png"/>
<meta property="og:site_name" content="Kallyas"/>
<meta property="fb:app_id" content=""/> <!-- PUT HERE YOUR OWN APP ID - you could get errors if you don't use this one -->
<meta property="og:description" content="Welcome to KALLYAS Template, a wonderful and premium product for multipurpose websites"/>
<!-- END Facebook OpenGraph Tags -->
<!-- THIS IS THE DARK THEME STYLESEET // REMOVE COMMENTS TO ENABLE -->
<!-- <link rel="stylesheet" href="css/dark-theme.css" type="text/css" /> -->
<!-- END DARK THEME -->
</head>
<body class="">
<!-- ADD AN APPLICATION ID !!
If you want to know how to find out your app id, either search on google for: facebook appid, either go to http://rieglerova.net/how-to-get-a-facebook-app-id/ -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "http://connect.facebook.net/en_US/all.js#xfbml=1&appId="; // addyour appId here
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="support_panel" id="sliding_panel">
<div class="container">
<div class="row">
<div class="span9">
<h4 class="m_title">HOW TO SHOP</h4>
<div class="m_content how_to_shop">
<div class="row">
<div class="span3">
<span class="number">1</span> Login or create new account.
</div>
<div class="span3">
<span class="number">2</span> Review your order.
</div>
<div class="span3">
<span class="number">3</span> Payment & <strong>FREE</strong> shipment
</div>
</div>
<p>If you still have problems, please let us know, by sending an email to support#website.com . Thank you!</p>
</div><!-- end how to shop steps -->
</div>
<div class="span3">
<h4 class="m_title">SHOWROOM HOURS</h4>
<div class="m_content">
Mon-Fri 9:00AM - 6:00AM<br>
Sat - 9:00AM-5:00PM<br>
Sundays by appointment only!
</div>
</div>Document.getElementById
</div>
</div>
</div><!-- end support panel -->
<div class="login_register_stuff hide"><!-- Login/Register Modal forms - hidded by default to be opened through modal -->
<div id="login_panel">
<div class="inner-container login-panel">
<h3 class="m_title">SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES</h3>
<form id="login_form" name="login_form" method="post" action="<?php echo base_url(); ?>index.php/verifylogin" onSubmit="checkLogin()">
Login with google
<input type="text" id="test_user" name="username" class="inputbox" placeholder="Username" onblur="checkEmpty(this.value)" />
<input type="password" id="password" name="password" class="inputbox" placeholder="Password" />
<input type="submit" id="login" name="submit" value="LOG IN">
Login with facebook
</form>
<!--
<div class="links">FORGOT YOUR USERNAME? / FORGOT YOUR PASSWORD?</div>
-->
</div>
</div><!-- end login panel -->
<div id="register_panel">
<div class="inner-container register-panel">
<h3 class="m_title">CREATE ACCOUNT</h3>
<form id="register_form" name="register_form" method="post">
<p>
<input type="text" id="reg-username" name="username" class="inputbox" placeholder="Username">
</p>
<p>
<input type="text" id="fullname" name="fullname" class="inputbox" placeholder="Your full name">
</p>
<p>
<input type="text" id="reg-email" name="email" class="inputbox" placeholder="Your email">
</p>
<p>
<input type="password" id="reg-password" name="password" class="inputbox" placeholder="Desired password">
</p>
<p>
<input type="password" id="confirm_password" name="confirm_password" class="inputbox" placeholder="Confirm password">
</p>
<p>
<input type="submit" id="signup" name="submit" value="CREATE MY ACCOUNT">
</p>
</form>
<div class="links">ALREADY HAVE AN ACCOUNT?</div>
</div>
</div><!-- end register panel -->
<div id="forgot_panel">
<div class="inner-container forgot-panel">
<h3 class="m_title">FORGOT YOUR DETAILS?</h3>
<form id="forgot_form" name="forgot_form" method="post">
<p>
<input type="text" id="forgot-email" name="email" class="inputbox" placeholder="Email Address">
</p>
<p>
<input type="submit" id="recover" name="submit" value="SEND MY DETAILS!">
</p>
</form>
<div class="links">AAH, WAIT, I REMEMBER NOW!</div>
</div>
</div><!-- end register panel -->
</div><!-- end login register stuff -->
<div id="page_wrapper">
<header id="header" class="style2">
<div class="container">
<!-- logo -->
<h1 id="logo"><a href="<?php echo base_url(); ?>" style="color:#fff">App Contest
</a></h1>
<ul class="topnav navRight">
<li><a href="#" id="open_sliding_panel">
<span class="icon-remove-circle icon-white"></span>
</a>
</li>
<?php if(isset($username))
{
echo'<li>LOGOUT</li>';
}
else
{
echo '<li>LOGIN</li>';
}?>
</ul><!-- end topnav // right aligned -->
<!-- end topnav // left aligned -->
<script type="text/javascript">
// THIS SCRIPT DETECTS THE ACTIVE ELEMENT AND ADDS ACTIVE CLASS
(function($){
$(document).ready(function(){
var pathname = window.location.pathname,
page = pathname.split(/[/ ]+/).pop(),
menuItems = $('#main_menu a');
menuItems.each(function(){
var mi = $(this),
miHrefs = mi.attr("href"),
miParents = mi.parents('li');
if(page == miHrefs) {
miParents.addClass("active").siblings().removeClass('active');
}
});
});
})(jQuery);
</script>
</div>
</header>
The function checkLogin() is in the file common_functions.js which I have included on line:33 and the login form is at line:129.
try this fiddle, it works for me
http://jsfiddle.net/WR2en/
as you can see the error was
onSubmit="javascript:checkLogin();">
try this
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>
<script>
function checkLogin()
{
var uname = jQuery('#test_user').val();
if(uname=='')
alert('Enter Username');
else
alert(uname);
//return true; // checklogin true
return false; // checklogin fail
}
</script>
</head>
<body>
<form id="login_form" name="login_form" method="post" action="verifylogin.php" onSubmit="return checkLogin()">
<input type="text" id="test_user" name="username" class="inputbox" placeholder="Username" value=""/>
<input type="password" id="password" name="password" class="inputbox" placeholder="Password" />
<input type="submit" id="login" name="submit" value="LOG IN">
Login with facebook
</form>
</body>
</html>

problem with my drop down login box?

i have a simple Jquery script which allow me to place a nice drop down login box but i have a simple issue with it
when i tried to place more than 1 box lets say 5 the script totally messed
i believe its something regarding the DIV id's and duplication but i don't find a way to resolve this problem
JS code
// Login Form
$(function() {
var button = $('#loginButton');
var box = $('#loginBox');
var form = $('#loginForm');
button.removeAttr('href');
button.mouseup(function(login) {
box.toggle();
button.toggleClass('active');
});
form.mouseup(function() {
return false;
});
$(this).mouseup(function(login) {
if(!($(login.target).parent('#loginButton').length > 0)) {
button.removeClass('active');
box.hide();
}
});
});
html code
<!doctype html>
<html>
<head>
<meta charset="utf8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>jQuery Dropdown Login Freebie | The Finished Box</title>
<link rel="stylesheet" href="css/style.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js?ver=1.4.2"></script>
<script src="js/login.js"></script>
</head>
<body>
<div id="bar">
<div id="container">
<!-- Login Starts Here -->
<div id="loginContainer">
<span>Login</span><em></em>
<div style="clear:both"></div>
<div id="loginBox">
<form id="loginForm">
<fieldset id="body">
<fieldset>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" />
</fieldset>
<fieldset>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</fieldset>
<input type="submit" id="login" value="Sign in" />
<label for="checkbox"><input type="checkbox" id="checkbox" />Remember me</label>
</fieldset>
<span>Forgot your password?</span>
</form>
</div>
</div>
<!-- Login Ends Here -->
</div>
</div>
</body>
</html>
Yes, Change the ID's and It will work for more than one.

Categories