This is a WEB DESIGN QUESTION ABOUT A JAVA SCRIPT. I have two JAVA script codes
ID: 3669831 • Letter: T
Question
This is a WEB DESIGN QUESTION ABOUT A JAVA SCRIPT. I have two JAVA script codes on my homepage and only one will work. If I delete A then B will work or If I delete B then A will work but when I have them both on my page then only the 2nd javascript code works. I can not figure out how to get them both to work. The first JS is located in <head> asks the user for their first name when they come to my website, it then take the name and save it as a cookie that is retrieved and shown to the user on the page upon subsequent visits with the greeting "Welcome back to our site. The 2nd JS is located in the <body> and is a a scrolling DHTML news area to highlight sales. Below is my homepage (index.shtm) both Javascripts codes are on this page. I would appreciate any help. Thanks so much
<!DOCTYPE html>
<html>
<head>
<meta name="language" content="english">
<meta charset="utf-8">
<title>Anarchy's Home Page</title>
<meta name= "author" content="SARAH">
<meta name= "copyright" content="SARAH 2016">
<meta name= "keywords" content="Anarchy, clothing, beachwear, shirts, t-shirts, dresses, boardshorts, sunglasses, accessories, hats, apparel">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name= "description" content="Anarchy's home page. In style beach apparel and accessories for men and women.">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="http://templatecycle.com/tshop/v-1.5/white/assets/css/style.css" >
<link rel="stylesheet" href="style.css">
<script>
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname+"="+cvalue+"; "+expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user=getCookie("username");
if (user != "") {
alert("Welcome back to our site " + user);
} else {
user = prompt("What is your name?","");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
}
</script>
</head>
<body>
<script src="script.js"></script>
<div id="wrapper">
<div id="callbox">
<header>
<img src="anarchy.png" alt="Anarchy logo."/>
<script type="text/JavaScript1.2">
//Specify the marquee's width (in pixels)
var marqueewidth="975px"
//Specify the marquee's height
var marqueeheight="25px"
//Specify the marquee's marquee speed (larger is faster 1-10)
var marqueespeed=2
//configure background color:
var marqueebgcolor="#FFF8DC"
//Pause marquee onMousever (0=no. 1=yes)?
var pauseit=1
//Specify the marquee's content (don't delete <nobr> tag)
//Keep all content on ONE line, and backslash any single quotations (ie: that's great):
var marqueecontent='<nobr><font face="Arial">Enter promo code SALE20 to recieve 20% off at checkout.                Free standard shipping off orders of $50 or more.                All boardshorts now 30% off. Discount applied at checkout.</font></nobr>'
////NO NEED TO EDIT BELOW THIS LINE////////////
marqueespeed=(document.all)? marqueespeed : Math.max(1, marqueespeed-1) //slow speed down by 1 for NS
var copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0
var iedom=document.all||document.getElementById
if (iedom)
document.write('<span id="temp">'+marqueecontent+'</span>')
var actualwidth=''
var cross_marquee, ns_marquee
function populate(){
if (iedom){
cross_marquee=document.getElementById? document.getElementById("iemarquee") : document.all.iemarquee
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
cross_marquee.innerHTML=marqueecontent
actualwidth=document.all? temp.offsetWidth : document.getElementById("temp").offsetWidth
}
else if (document.layers){
ns_marquee=document.ns_marquee.document.ns_marquee2
ns_marquee.left=parseInt(marqueewidth)+8
ns_marquee.document.write(marqueecontent)
ns_marquee.document.close()
actualwidth=ns_marquee.document.width
}
lefttime=setInterval("scrollmarquee()",20)
}
window.onload=populate
function scrollmarquee(){
if (iedom){
if (parseInt(cross_marquee.style.left)>(actualwidth*(-1)+8))
cross_marquee.style.left=parseInt(cross_marquee.style.left)-copyspeed+"px"
else
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
}
else if (document.layers){
if (ns_marquee.left>(actualwidth*(-1)+8))
ns_marquee.left-=copyspeed
else
ns_marquee.left=parseInt(marqueewidth)+8
}
}
if (iedom||document.layers){
with (document){
document.write('<table border="0" cellspacing="0" cellpadding="0"><td>')
if (iedom){
write('<div>')
write('<div>')
write('<div id="iemarquee"></div>')
write('</div></div>')
}
else if (document.layers){
write('<ilayer width='+marqueewidth+' height='+marqueeheight+' name="ns_marquee" bgColor='+marqueebgcolor+'>')
write('<layer name="ns_marquee2" left=0 top=0></layer>')
write('</ilayer>')
}
document.write('</td></table>')
}
}
</script>
</header>
<nav>
<ul>
<li><a href="index.shtm">Home</a></li>
<li><a href="About.shtm">About</a></li>
<li><a href="Contact.shtm">Contact</a></li>
<li><a href="index.shtm#shop">Shop ▾</a>
<ul class="dropdown">
<li><a href="men.shtm">Mens</a></li>
<li><a href="women.shtm">Womens</a></li>
<li><a href="Accessories.shtm">Accessories</a></li>
</ul>
</li>
</ul>
</nav>
</div>
<div class="clearfix"></div>
<div class="banner">
<img src="Banner.jpg" alt="Girl on beach with hat"/>
</div>
<div class="container1">
<a href="SizeChart.shtm" class="button">Click For Size Chart</a>
</div>
<div class="container">
<h3>Shop Mens</h3>
<a href="men.shtm"><img src="MensApparel1.jpg" alt="Guy wearing beach apparel"/></a>
</div>
<div class="container">
<h3>Shop Accessories</h3>
<a href="Accessories.shtm"><img src="Access1.jpg" alt="Hat, Sunglasses and a Bangle bracelet"/></a>
</div>
<div class="container">
<h3>Shop Womens</h3>
<a href="women.shtm"><img src="WomanApparel.jpg" alt="Girl wearing beach apparel"/></a>
</div>
<a id="shop"></a>
<footer>
<div class="container">
<p>Clear Water Location</p>
<p>813-555-5555<br>
714 Gulf Blvd<br>
Clearwater Beach, FL 33767<br>
</div>
<div class="container">
<p>AnarchyBeachWear@gmail.com</p>
<p>Follow Us On</p>
<ul>
<li><a href="https://www.facebook.com/"><img src="facebook1.png" alt="Facebook logo"/></a></li>
<li><a href="https://plus.google.com/"><img src="googleplus.png" alt="Google logo"/></a></li>
<li><a href="https://twitter.com"><img src="twitter1.png" alt="Twitter logo"/></a></li>
</ul>
</div>
<div class="container">
<p>Daytona Beach Location</p>
<p>386-555-5555<br>
505 Atlantic Ave<br>
Daytona Beach, FL 32713<br></p>
</div>
</footer>
</div>
<p>©Copyright - Anarchy by Gina Eberhardt, 2016.</p>
</body>
</html>
Explanation / Answer
Call the fuction checkCookie() in the function populate() instead of calling it in the body tag.
problem is "window.onload" and the "onload" funtion in the body tag are colliding.
find the below changed code for the reference.
--------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<meta name="language" content="english">
<meta charset="utf-8">
<title>Anarchy's Home Page</title>
<meta name= "author" content="SARAH">
<meta name= "copyright" content="SARAH 2016">
<meta name= "keywords" content="Anarchy, clothing, beachwear, shirts, t-shirts, dresses, boardshorts, sunglasses, accessories, hats, apparel">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name= "description" content="Anarchy's home page. In style beach apparel and accessories for men and women.">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="http://templatecycle.com/tshop/v-1.5/white/assets/css/style.css" >
<link rel="stylesheet" href="style.css">
<script>
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname+"="+cvalue+"; "+expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user=getCookie("username");
if (user != "") {
alert("Welcome back to our site " + user);
} else {
user = prompt("What is your name?","");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
}
</script>
</head>
<body>
<script src="script.js"></script>
<div id="wrapper">
<div id="callbox">
<header>
<img src="anarchy.png" alt="Anarchy logo."/>
<script type="text/JavaScript1.2">
//Specify the marquee's width (in pixels)
var marqueewidth="975px"
//Specify the marquee's height
var marqueeheight="25px"
//Specify the marquee's marquee speed (larger is faster 1-10)
var marqueespeed=2
//configure background color:
var marqueebgcolor="#FFF8DC"
//Pause marquee onMousever (0=no. 1=yes)?
var pauseit=1
//Specify the marquee's content (don't delete <nobr> tag)
//Keep all content on ONE line, and backslash any single quotations (ie: that's great):
var marqueecontent='<nobr><font face="Arial">Enter promo code SALE20 to recieve 20% off at checkout.                Free standard shipping off orders of $50 or more.                All boardshorts now 30% off. Discount applied at checkout.</font></nobr>'
////NO NEED TO EDIT BELOW THIS LINE////////////
marqueespeed=(document.all)? marqueespeed : Math.max(1, marqueespeed-1) //slow speed down by 1 for NS
var copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0
var iedom=document.all||document.getElementById
if (iedom)
document.write('<span id="temp">'+marqueecontent+'</span>')
var actualwidth=''
var cross_marquee, ns_marquee
function populate(){
if (iedom){
cross_marquee=document.getElementById? document.getElementById("iemarquee") : document.all.iemarquee
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
cross_marquee.innerHTML=marqueecontent
actualwidth=document.all? temp.offsetWidth : document.getElementById("temp").offsetWidth
}
else if (document.layers){
ns_marquee=document.ns_marquee.document.ns_marquee2
ns_marquee.left=parseInt(marqueewidth)+8
ns_marquee.document.write(marqueecontent)
ns_marquee.document.close()
actualwidth=ns_marquee.document.width
}
lefttime=setInterval("scrollmarquee()",20)
checkCookie()
}
window.onload=populate
function scrollmarquee(){
if (iedom){
if (parseInt(cross_marquee.style.left)>(actualwidth*(-1)+8))
cross_marquee.style.left=parseInt(cross_marquee.style.left)-copyspeed+"px"
else
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
}
else if (document.layers){
if (ns_marquee.left>(actualwidth*(-1)+8))
ns_marquee.left-=copyspeed
else
ns_marquee.left=parseInt(marqueewidth)+8
}
}
if (iedom||document.layers){
with (document){
document.write('<table border="0" cellspacing="0" cellpadding="0"><td>')
if (iedom){
write('<div>')
write('<div>')
write('<div id="iemarquee"></div>')
write('</div></div>')
}
else if (document.layers){
write('<ilayer width='+marqueewidth+' height='+marqueeheight+' name="ns_marquee" bgColor='+marqueebgcolor+'>')
write('<layer name="ns_marquee2" left=0 top=0></layer>')
write('</ilayer>')
}
document.write('</td></table>')
}
}
</script>
</header>
<nav>
<ul>
<li><a href="index.shtm">Home</a></li>
<li><a href="About.shtm">About</a></li>
<li><a href="Contact.shtm">Contact</a></li>
<li><a href="index.shtm#shop">Shop ▾</a>
<ul class="dropdown">
<li><a href="men.shtm">Mens</a></li>
<li><a href="women.shtm">Womens</a></li>
<li><a href="Accessories.shtm">Accessories</a></li>
</ul>
</li>
</ul>
</nav>
</div>
<div class="clearfix"></div>
<div class="banner">
<img src="Banner.jpg" alt="Girl on beach with hat"/>
</div>
<div class="container1">
<a href="SizeChart.shtm" class="button">Click For Size Chart</a>
</div>
<div class="container">
<h3>Shop Mens</h3>
<a href="men.shtm"><img src="MensApparel1.jpg" alt="Guy wearing beach apparel"/></a>
</div>
<div class="container">
<h3>Shop Accessories</h3>
<a href="Accessories.shtm"><img src="Access1.jpg" alt="Hat, Sunglasses and a Bangle bracelet"/></a>
</div>
<div class="container">
<h3>Shop Womens</h3>
<a href="women.shtm"><img src="WomanApparel.jpg" alt="Girl wearing beach apparel"/></a>
</div>
<a id="shop"></a>
<footer>
<div class="container">
<p>Clear Water Location</p>
<p>813-555-5555<br>
714 Gulf Blvd<br>
Clearwater Beach, FL 33767<br>
</div>
<div class="container">
<p>AnarchyBeachWear@gmail.com</p>
<p>Follow Us On</p>
<ul>
<li><a href="https://www.facebook.com/"><img src="facebook1.png" alt="Facebook logo"/></a></li>
<li><a href="https://plus.google.com/"><img src="googleplus.png" alt="Google logo"/></a></li>
<li><a href="https://twitter.com"><img src="twitter1.png" alt="Twitter logo"/></a></li>
</ul>
</div>
<div class="container">
<p>Daytona Beach Location</p>
<p>386-555-5555<br>
505 Atlantic Ave<br>
Daytona Beach, FL 32713<br></p>
</div>
</footer>
</div>
<p>©Copyright - Anarchy by Gina Eberhardt, 2016.</p>
</body>
</html>
-------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.