JavaScript function not work onload - javascript

I have write a JavaScript code to perform a slide show but it's not working onload when i assign it to a <a> it works but not work onload
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Slide Show</title>
<meta name="author" content="Jenz" />
<!-- Date: 2014-07-18 -->
<script>
var slideImg;
var picNo = 0;
window.onload = function() {
slideImg = document.getElementById('slideShowImage');
images = new Array();
images[0] = new Image();
images[0].src = "Images/image1.jpg";
images[1] = new Image();
images[1].src = "Images/image2.jpg";
images[2] = new Image();
images[2].src = "Images/image3.jpg";
};
function slide() {
slideImg.src = images[picNo].src;
if (picNo < 2) {
picNo++;
} else {
picNo = 0;
}
timer = setTimeout(slide, 1000);
};
</script>
</head>
<body>
<div style="margin-left: auto; margin-right: auto; width: 900px">
<div id="slideShowAndNav" style="margin: auto; width: 700px;">
<img id="slideShowImage" name="slideshow" src="Images/image1.jpg" style="border-radius: 0px 0px 0px 250px; position: absolute; top: -50; left: -50; z-index: 1;" border="1px" />
<a style="display: block; height: 40px; width: 40px; background-color: red; border-radius: 20px; text-align: center; position: absolute; z-index: 2; margin-top: 130px;" href="">Home</a>
</div>
</div>
</body>
</html>
Please look at the code and tell me what should i do and other thing i have a navigation which code is
<a style="display: block; height: 40px; width: 40px; background-color: red; border-radius: 20px; text-align: center; position: absolute; z-index: 2; margin-top: 130px;" href="">Home</a>
i want this <a> in a circle as i did with border-radius and now i want it's
Text in the center like Home in the middle of the circle help me also with this

i want this in a circle as i did with border-radius and now i want it's Text in the center like Home in the middle of the circle help me also with this
For this, use:-
style="border-radius:100px; text-align:center;"

The "xmlns" attribute is invalid in HTML 4.01. (You can change your doctype.)
Also, it is more common practice to create your function.
function blaah(blaah){
blaah
}
And then add an onload event handler, either with an event listener, or add this code to your body tag,
<body onload="blaah('blaah')">
or, you could add this code in your script tags, but outside of the function.
blaah("blaah");

Call slide function.
window.onload = function() {
slideImg = document.getElementById('slideShowImage');
images = new Array();
images[0] = new Image();
images[0].src = "Images/image1.jpg";
images[1] = new Image();
images[1].src = "Images/image2.jpg";
images[2] = new Image();
images[2].src = "Images/image3.jpg";
slide();
};

Related

HTML + JS, overlay an uploaded image onto a canvas

Sorry if this is a duplicate I'm new to JS and HTML and I can't figure out what else to do.
I am trying to make (mostly from JS) a way to upload images onto any background you like, then saving them as one image. For now, I am experimenting by putting an image of the scream onto a canvas and then trying to upload an image of my choosing. Currently when I press save image, it saves ONLY the background. Also the background and uploaded image both overflow out of the card, but removing position:absolute in css stops them from overlaying. I've tried stuff with MarvinJ or even just trying to not use canvas, but can't get it to work.
Sorry for the big question, how do I merge an uploaded image and an image of my choosing onto a card and save them as one file?
HTML:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta name = "viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type = "text/css" href="blank.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<title> Petra image </title>
</head>
<body>
<div class = "wrap">
<div class = "card-container">
<input type="file" onchange="previewFile()"><br>
<img src="" height="200" class = "Card_Image" alt="Image preview...">
</div>
<div class="card_main">
<div id="picHolder">
<img src="" id = "pic" class = "test" height="300" alt="Image preview2" object-fit: cover;>
<img id="scream" class = "scream" width="300" height="480" src="img_the_scream.jpg" alt="The Scream" object-fit: cover;>
</div>
</div>
</div>
</div>
<script src="blank.js"></script>
</body>
CSS:
html, body {
margin: 0;
width:100%;
padding:0;
}
.wrap {
display: flex;
flex-direction: column;
}
.card-container {
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
}
.card_main {
width: 300px;
height: 420px;
margin: 12px;
background-color: rgb(0, 0, 0);
padding: 10px;
overflow: hidden;
}
.scream{
position: absolute;
opacity: 0.2;
border: 1px solid #000000;
z-index: 1;
}
Javascript:
function previewFile() {
var preview = document.querySelector('img');
var preview2 = document.querySelector('.test')
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
preview2.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
}
window.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("pic");
var img2 = document.getElementById("scream2");
ctx.drawImage(img, 10, 10);
ctx.globalAlpha = 0.5;
ctx.drawImage(img2, 10, 10);
}

Why isn't my slideshow not working within my HTML?

I coded a simple slideshow with Javascript but it isn't working when I place it within my HTML page that I have created. So far I have tried to include it in different areas, different div's and from an external file. Nothing seems to be working. I have tested the code on blank HTML and it works. But when I use it within the page that I have put together, it wont load.
var i = 0; // Start Point
var images = []; // Images Array
var time = 3000; // Time Between Switch
// Image List
images[0] = "gotpic1.jpg";
images[1] = "gotpic2.jpg";
// Change Image
function changeImg(){
document.slide.src = images[i];
// Check If Index Is Under Max
if (i < images.length - 1){
// Add 1 to Index
i++;
} else {
// Reset Back To O
i = 0;
}
// Run function every x seconds
setTimeout("changeImg()", time);
}
// Run function when page loads
window.onload = changeImg;
body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
background-repeat:no-repeat;
background-size: cover;
}
.gotlogo{
text-align: center;
}
.slider {
border: solid black 2p
}
.slider{
height: 300px;
width: 500px;
}
<body style=" background-image: url("forest1.jpg");">
<div class="gotlogo">
<img src="gotlogo2.png" alt="GOT">
</div>
<div class="slider">
<img name="slide" height="200" width="400">
</div>
<p>
<br>
</p>
</body>
Although there are a lot to of room for improvements, for now I'm giving you a working prototype of your version of code. Just added an id to the <img/> and how src is manipulated in changeImg()
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body style=" background-image: url("forest1.jpg");">
<div class="gotlogo"> <img src="gotlogo2.png" alt="GOT"> </div>
<div class="slider">
<img name="slide" id="slide" height="200" width="400">
</div>
<p><br>
</p>
<script>
var i = 0; // Start Point
var images = []; // Images Array
var time = 3000; // Time Between Switch
// Image List
images[0] = "http://www.buyersgohappy.com/blog/wp-content/uploads/2017/08/GOT-Kingdom-in-India-11-750x339.jpg";
images[1] = "https://i.kym-cdn.com/entries/icons/mobile/000/010/576/got.jpg";
// Change Image
function changeImg() {
document.getElementById("slide").src = images[i];
// Check If Index Is Under Max
if (i < images.length - 1) {
// Add 1 to Index
i++;
} else {
// Reset Back To O
i = 0;
}
// Run function every x seconds
setTimeout("changeImg()", time);
}
// Run function when page loads
window.onload = changeImg;
</script>
<style>
body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
background-repeat: no-repeat;
background-size: cover;
}
.gotlogo {
text-align: center;
}
.slider {
border: solid black 2px
}
.slider {
height: 300px;
width: 500px;
}
</style>
</body>
</html>
Having that said, let's see how you can improve your code. Please checkout the changeImg() function:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body style=" background-image: url("forest1.jpg");">
<div class="gotlogo"> <img src="gotlogo2.png" alt="GOT"> </div>
<div class="slider">
<img name="slide" id="slide" height="200" width="400">
</div>
<p><br>
</p>
<script>
var i = 0; // Start Point
var images = []; // Images Array
var time = 3000; // Time Between Switch
// Image List
/*
For local images:
say you have a local folder structure like this:
GOT
|
--> index.html (this file)
|
--> Images
|
--> got1.jpg
|
--> got2.jpg
Now in you can can these images like:
images[0] = "Images/got1.jpg"
images[1] = "Images/got2.jpg"
Let's try another example folder structure:
GOT
|
--> Html
| |
| --> index.html (this file)
|
--> Images
|
--> got1.jpg
|
--> got2.jpg
Now in you can can these images like:
images[0] = "../Images/got1.jpg"
images[1] = "../Images/got2.jpg"
*/
images[0] = "http://www.buyersgohappy.com/blog/wp-content/uploads/2017/08/GOT-Kingdom-in-India-11-750x339.jpg";
images[1] = "https://i.kym-cdn.com/entries/icons/mobile/000/010/576/got.jpg";
// Change Image
function changeImg() {
document.getElementById("slide").src = images[i];
// Keep index under the size of images array
i = (i+1) % images.length;
}
// Run function when page loads
window.onload = function() {
// Run function every x seconds
setInterval(changeImg, time);
}
</script>
<style>
body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
background-repeat: no-repeat;
background-size: cover;
}
.gotlogo {
text-align: center;
}
.slider {
border: solid black 2px
}
.slider {
height: 300px;
width: 500px;
}
</style>
</body>
</html>
u are using old fashioned way u maybe be practicing i just changed a bitin code
i used getElementsByName() function to find element
var i = 0; // Start Point
var images = []; // Images Array
var time = 3000; // Time Between Switch
// Image List
images[0] = "https://www.w3schools.com/html/pic_trulli.jpg";
images[1] = "https://www.w3schools.com/html/img_girl.jpg";
// Change Image
function changeImg(){
document.getElementsByName('slide')[0].src = images[i];
// Check If Index Is Under Max
if (i < images.length - 1){
// Add 1 to Index
i++;
} else {
// Reset Back To O
i = 0;
}
// Run function every x seconds
setTimeout("changeImg()", time);
}
// Run function when page loads
window.onload = changeImg;
body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
background-repeat:no-repeat;
background-size: cover;
}
.gotlogo{
text-align: center;
}
.slider {
border: solid black 2p
}
.slider{
height: 300px;
width: 500px;
}
<body style=" background-image: url("forest1.jpg");">
<div class="gotlogo">
<img src="gotlogo2.png" alt="GOT">
</div>
<div class="slider" name='w'>
<img name="slide" height="200" width="400">
</div>
<p>
<br>
</p>
</body>

Put image on div randomly using javascript

I am making matching game that images in left and right are different, and users have to find(I put one more image in left side).
My problem is that I cannot call image on where I want. What should I have to fix? Thank you.
<!DOCTYPE html>
<html>
<head>
<style>
img {
position: absolute;
}
div {
position: absolute;
width: 500px;
height: 500px;
}
#leftside {
float: left;
}
#rightside {
float: right;
left: 500px;
border-left: 1px solid black
}
</style>
<script>
var numberOfFaces(5);
var theLeftSide = document.getElementById("leftSide");
function generateFaces() {
var createElement("img");
var position = Math.floor(Math.random() * 500);
img.src = 'http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png';
img.id = 'smileImage';
createElement.setAttribute("height", position);
createElement.setAttribute("width", position);
document.getElementById('leftSide').appendChild(img);
}
</script>
</head>
<body>
<h1>Matching Game</h1>
<p>Click on the extra smiling face on the left.</p>
<div id="leftside"></div>
<div id="rightside"></div>
</body>
</html>
You need to set something to the result of createElement('img'). For example, you can change that line to var img = createElement('img');
You never actually use theLeftSide.
createElement is actually document.createElement.
You never defined the variable createElement. Since we called it img earlier, do so now.
leftSide is not the ID of any element, but leftside is. Change it so that all usages are capitalized the same.
After that, you actually need to call generateFaces. Because the page is loaded top-to-bottom, JavaScript doesn't yet know about the div with the ID of leftSide. You have to wait until the page is finished loading to call the Javascript, and you can do that by adding window.onload = generateFaces to the end of your script.
Here's the final result:
<!DOCTYPE html>
<html>
<head>
<style>
img {
position: absolute;
}
div {
position: absolute;
width: 500px;
height: 500px;
}
#leftside {
float: left;
}
#rightside {
float: right;
left: 500px;
border-left: 1px solid black
}
</style>
<script>
function generateFaces() {
var img = document.createElement('img');
var position = Math.floor(Math.random() * 500);
img.src = 'http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png';
img.id = 'smileImage';
img.setAttribute('height', position);
img.setAttribute('width', position);
document.getElementById('leftSide').appendChild(img);
}
window.onload = generateFaces;
</script>
</head>
<body>
<h1>Matching Game</h1>
<p>Click on the extra smiling face on the left.</p>
<div id="leftSide"></div>
<div id="rightSide"></div>
</body>
</html>

Canvas not showing background?

I am using EaselJS, and for some reason my canvas isn't showing the image as a background even though this is how tutorials do it. My HTML file is as so:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Requiescat in Pace</title>
...lots of .js imports for EaselJS
<script type="text/javascript" src="RIPwitheasel.js"></script>
<style>
canvas {
position: absolute;
left: 0;
right: 0;
top: 10%;
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
width: 800px;
background: transparent;
}
</style>
<link rel="stylesheet" type="text/css" href="style_2.css">
</head>
<body onload="init()">
<font color = white>
<img alt = "full screen bg img" src = "images/backgrounds/pagebackground.jpg" id = "full-screen-background-image"/>
<div style="position: relative">
<h1 style="position: fixed; width:100%; text-align: center"> <strong> Requiescat In Pace </strong> </h1>
<p style = "text-align: center"> Prototype 1 </p>
</div>
<form action="" style = "position: fixed; top: 0; width: 100% text-align: center" >
<input type="button" value="Donate!!!">
</form>
<div style="position: relative">
<p style="position: fixed; bottom: 0; width:100%; text-align: center"> Credits: To be added</p>
</div>
<canvas id="gameCanvas" width="1000" height="736">
Your browser does not support canvas. Please try again with a different browser.
</canvas>
</font>
</body>
</html>
And my RIPwitheasel.js is this:
var stage;
function init()
{
var canvas = document.getElementById("gameCanvas");
stage = new createjs.Stage(canvas);
var background = new createjs.Bitmap("images/backgrounds/grass-tiled.png");
stage.addChild(background);
createjs.Ticker.setFPS(60);
stage.update();
}
function tick()
{
stage.update();
}
What is it that is incorrectly defined? All of the page displays correctly but the canvas doesn't display the image. I get zero run-time errors in the browser console for the js file.
EDIT: I just realized that if I use a .jpg file instead of a .png file it loads it. Why is this?
See this answer from CreateJS developer: https://stackoverflow.com/a/20682883/1996480
Here's how you could use PreloadJS to load your background:
var queue = new createjs.LoadQueue();
queue.on("complete", handleComplete, this);
queue.loadFile({id:"myBackground", src:"images/backgrounds/grass-tiled.png"});
function handleComplete() {
var image = queue.getResult("myBackground");
stage.addChild(image);
}

Position elements on screen using Javascript

I am attempting to use Javascript to position some of my elements on the screen/window. I am doing this to make sure that what ever the dimensions of the users screen, my elements will always be in the centre.
I know that padding & margin can also achieve this, but I am using a custom movement script called raphael.js, & in order to move my elements I need to set out my elements absolutely (its a custom home page, where you click blocks(that are links) & they fly off the screen).
My javascript function to move my elements fails to move my elements. Any suggestions on how to position my elements using javascript would be really helpful.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="dropDownMenu.js"></script>
<title></title>
<script src="javascript/raphael.js"></script> <!-- I am using a custom drawing/moving script which is why I cant put the img(id=bkImg) inside the div element -->
<script LANGUAGE="JavaScript" type = "text/javascript">
<!--
function getScreenSize()
{
var res = {"width": 630, "height": 460};
if (document.body)
{
if (document.body.offsetWidth) res["width"] = document.body.offsetWidth;
if (document.body.offsetHeight) res["height"] = document.body.offsetHeight;
}
if (window.innerWidth) res["width"] = window.innerWidth;
if (window.innerHeight) res["height"] = window.innerHeight;
alert( res["width"] );
alert( res["height"] );
return res;
}
function positionBlocksAccordingToScreenSize()
{
// The problem is that the blocks position does not change but they should?
var scrDim = getScreenSize();
var BK_WIDTH = 800;
var BK_HEIGHT = 600;
var X_OFFSET = (scrDim["width"]-BK_WIDTH) / 2; // variable that changes according to the width of the screen
var BLOCK_POS_X = [160, 80, 280, 20, 200, 400];
var BLOCK_POS_Y = [26, 203, 203, 380, 380, 380];
for ( var i=1; i<=5; i++ )
{
//document.getElementById("block"+i).setAttribute( "offsetLeft", X_OFFSET + BLOCK_POS_X[i] ); // doesn't work
//document.getElementById("block"+i).setAttribute( "offsetTop", BLOCK_POS_Y[i] ); // doesn't work
document.getElementById("block"+i).style.left = X_OFFSET + BLOCK_POS_X[i]; // doesn't work
document.getElementById("block"+i).style.top = BLOCK_POS_Y[i]; // doesn't work
}
}
-->
</script>
<style type="text/css" media="all">
<!--
#import url("styles.css");
#blockMenu { z-index: -5; padding: 0; position: absolute; width: 800px;
height: 600px; /*background-image: url("images/menuBk.png");*/ }
#bkImg { z-index: -5; position: relative; }
#block1 { z-index: 60; position: absolute; top: 26px; left: 160px; margin: 0; padding: 0; }
#block2 { z-index: 50; position: absolute; top: 203px; left: 80px; margin: 0; padding: 0; }
#block3 { z-index: 40; position: absolute; top: 203px; left: 280px; margin: 0; padding: 0; }
#block4 { z-index: 30; position: absolute; top: 380px; left: 20px; margin: 0; padding: 0; }
#block5 { z-index: 20; position: absolute; top: 380px; left: 200px; margin: 0; padding: 0; }
#block6 { z-index: 10; position: absolute; top: 380px; left: 400px; margin: 0; padding: 0; }
-->
</style>
</head>
<body onload="positionBlocksAccordingToScreenSize()" style="margin-top: 10%; text-align: center; margin-bottom: 10%; position: relative;">
<img id="bkImg" src="images/menuBk.png" width="800px;" height="600px" alt=""/>
<!-- The above image should be displayed behind the below div. I am using the raphael.js movement script, so I cannot place
this image inside the div, because it will get erased when I call raphael.clear(); -->
<div id="blockMenu">
<div id="block1"><img src="images/block1.png" width="200" height="200"/></div>
<div id="block2"><img src="images/block2.png" width="200" height="200"/></div>
<div id="block3"><img src="images/block3.png" width="200" height="200"/></div>
<div id="block4"><img src="images/block4.png" width="200" height="200"/></div>
<div id="block5"><img src="images/block5.png" width="200" height="200"/></div>
<div id="block6"><img src="images/block6.png" width="200" height="200"/></div>
</div>
</body>
</html>
You're not setting the left and top properties correctly, you need to add the unit e.g 'px' for pixels.
document.getElementById("block"+i).style.left = X_OFFSET + BLOCK_POS_X[i] + 'px';
document.getElementById("block"+i).style.top = BLOCK_POS_Y[i] + 'px';
"Would you be able to suggest a line of jQuery code that could place my elements correctly?"
The following code uses jQuery and will center an absolutely positioned element on load and on window resize.
http://jsfiddle.net/Fu3L6/
HTML...
<div id="element">Test</div>
CSS...
#element {
position: absolute;
background-color: red;
width: 200px;
}
jQuery...
$(document).ready(function () {
centerInViewport('#element');
$(window).resize(function () {
centerInViewport('#element');
});
});
function centerInViewport(e) {
$docWidth = $(document).width();
$elWidth = $(e).width();
$offset = ($docWidth - $elWidth) / 2;
$(e).css("marginLeft", $offset + "px");
}

Categories

Resources