Javascript modifying img src with substring - javascript

i'm trying to 'cut' the hash of some urls in iteration, leaving only the img url (https://i.ytimg.com/vi/9Eu7SXGyvzQ/hqdefault.jpg) without the hash (?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLDi79vN15idfFETvntyC9yat7FvZQ).
I got it working in this example (2 images) but in "real code" fail, as you can see they are still some issues in code I cannot figure out, maybe redundancy, not calling the src string correctly.
Need some advice.
Thanks.
function changeSourceAll() {
var images = document.querySelectorAll("#img,.style-scope,.yt-img-shadow");
for (var i = 0; i <= images.length; i++) {
var mini = images[i].src.substring(48);
var source = images[i].src = images[i].src.replace(mini, "");
}
}
changeSourceAll();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sometitle</title>
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<img id="img" draggable="false" class="style-scope yt-img-shadow" alt="" width="210" src="https://i.ytimg.com/vi/9Eu7SXGyvzQ/hqdefault.jpg?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLDi79vN15idfFETvntyC9yat7FvZQ">
<img id="img" draggable="false" class="style-scope yt-img-shadow" alt="" width="210" src="https://i.ytimg.com/vi/gCI1L0gP-vk/hqdefault.jpg?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLAUz6VUtsyrv5VZcfRvtnYM48ymHA">
<script src="java.js"></script>
</main>
</body>
</html>

Use a common selector to get all your img elements: for instance, you could rely on the class .yt-img-shadow.
Then use split() to divide each image src URL in two parts, separated by the symbol ?, and re-assign the "short" version of your URL to the image.
function changeSourceAll() {
const images = document.querySelectorAll(".yt-img-shadow");
for (let i = 0; i < images.length; i++) {
const mini = images[i].src.split('?')[0];
images[i].src = mini;
}
}
changeSourceAll();
<main>
<img id="img1" draggable="false" class="style-scope yt-img-shadow" alt="" width="210" src="https://i.ytimg.com/vi/9Eu7SXGyvzQ/hqdefault.jpg?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLDi79vN15idfFETvntyC9yat7FvZQ">
<img id="img2" draggable="false" class="style-scope yt-img-shadow" alt="" width="210" src="https://i.ytimg.com/vi/gCI1L0gP-vk/hqdefault.jpg?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLAUz6VUtsyrv5VZcfRvtnYM48ymHA">
</main>

Related

Chrome: Changing img src repeatedly causes chrome to request image again?

Same title. I'm not sure if this is a browser bug or behavior, see below example:
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
var srcs = [
'https://i.stack.imgur.com/sIH1O.jpg?s=48&g=1',
'https://www.gravatar.com/avatar/9aa2aa68144b2781ac025a29c83bbdd4?s=32&d=identicon&r=PG',
'https://www.gravatar.com/avatar/c0377fc1b08293d3362611978913a32b?s=32&d=identicon&r=PG',
'https://www.gravatar.com/avatar/07f36acea2b942f61102221f92f62f6e?s=32&d=identicon&r=PG',
//
// 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALgAAAA8CAYAAADVEnAJAAAOvklEQVR42u1dCZBcRRluyM4Sbg9QuUTFYAhy7Zs3G2Nw5r2ZTWKMWBCXQ5QzIncEFIqjGGtnZpdwaEUOIYcFlBwVRBA5wh7hUIIQCFgkJCAWBUWSnZ3N9d7MXgk7/p/sZje72/3umR2rv6quDOyb1zXd3/v77+///37MD0STxapYY6FWT+ev0zP5h7W0sTqWyWdjGaNA/13UU0Yf/ZvT08bbWjr/GH2+Qc8YJyv3FUNMQmK8Qk8XIrGM+Qc9ld8CIjttn33PvC/elD+JSUiMFxAp64igL4GkvrW08TytADVMouIQfnRucazGKg11qe6v62nzryBkIC1l9NO/d0eT2f2YhCR4ia32T2M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bol8P42ihqUTJ4JQlKgw18SkqJD72b0EcXrujPU2/4h6X4eW2VMVXHW05IC1G75VA9wNuc/tIAfv1BVhigHToRodffSC6Vx5LX+HPGVYOYLS+UkG1j4kgh+rO2geFzvLqeC52EpQO9VpO1PUxQVMcpkioWnmjql27jtjCP8rsgXPBbIBPSSJE1SWVYpa30JhzGcUw5E3mSEgy5mYGYkO5cLSFf2glPVjdwI1lrzXXcn8Pz029uu5dHqurtWoe4tXJDWWK8xblqJoYmnlNRAxMdXC8QjMdQjLIPiIyjlSubJJTMOVQvGazU8pRMo+Fy3lt9GjPPq3/ABI9Uety4aRGrbDbtL2vRLNN/TrTUVMsNXVz4SaZlmzhwgESjkLluTuPTM6mVlOSLjBMSp5M/mVLjyGw28z5NaEnsTdqmw6wl+KGGaojfMbcgOAQCfuR0SYUBLl8sQP11UuonKnrDEk+20OGuqXw/FRSxybYmLHCE4IPBH89VQjxEW06Xlm0a1HVpJNLWrJJOUj2bmck8Ae2OMY/QddHQI7eUiDQ1DLDTPGs8ADo8ZIJIwwYEc/fOf1x1zwrwPul5uXcCGi0zfbjd7zYNJUslo07RVtRJPHYqUSrPJSwhvpTyMrryTtKrnaigIwGkI8JU+nJaDVgpGE0lJQu4jSQHdgoHDbvzDc2Bk8Y/bpY6HztxbIf9fQvVNLBFVQRujbDtV43wxP8MP8Azqxw5fiIhuYNI//Z',
];
var $x = $('#x');
var i = -1;
setInterval(function() {
++i;
if(i >= 4) {
i = 0;
}
$x.attr('src', srcs[i]);
}, 100);
});
</script>
</head>
<body>
<img id="x" width="50" height="50">
</body>
</html>
Every few random seconds, the images are requested again, although fetched from disk cache, but request is happening. If you resize window, the requests may happen even more frequently.
Check JS Fiddle
I tried using base64 images instead of URLs, still same thing. This is an issue because the visible image may glitch/flicker when request occurs.
EDIT
How to prevent chrome from requesting the same image again?
This is the right behavior as "Memory Cache" stores and loads resources to and from Memory (RAM). So this is much faster but it is non-persistent. Content is available until you close the Browser.
"Disk Cache" is persistent. Cached resources are stored and loaded to and from disk.
For more info on how Chrome caches works (memory and disk), please refer to this link.
Placing multiple imgs, hide all and show only 1 at a time, prevents Chrome from requesting same images. With this approach, each image is requested only once.
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
var i = -1;
setInterval(function() {
++i;
if(i >= 4) {
i = 0;
}
$('.img').hide();
$('#img' + i).show();
}, 100);
});
</script>
</head>
<body>
<!-- using multiple images with display: none, src is fixed in HTML (not changed by JS) -->
<img id="img0" class="img" width="50" height="50" style="display: none;" src="https://i.stack.imgur.com/sIH1O.jpg?s=48&g=1">
<img id="img1" class="img" width="50" height="50" style="display: none;" src="https://www.gravatar.com/avatar/9aa2aa68144b2781ac025a29c83bbdd4?s=32&d=identicon&r=PG">
<img id="img2" class="img" width="50" height="50" style="display: none;" src="https://www.gravatar.com/avatar/c0377fc1b08293d3362611978913a32b?s=32&d=identicon&r=PG">
<img id="img3" class="img" width="50" height="50" style="display: none;" src="https://www.gravatar.com/avatar/07f36acea2b942f61102221f92f62f6e?s=32&d=identicon&r=PG">
</body>
</html>
JS Fiddle

click handler to enlarge image

I cant change the html or css files. My task is to set a click handler to my thumbnails to enlarge the image in the img within the element. While also setting the figcaption text in the figure to the thumbs title attribute. I need to attach to the div id = thumbnails. My script is not enlarging my thumbnails or titles.
This is what I have so far.
<!DOCTYPE html>
<html lang="en">
<head >
<meta charset="utf-8">
<title>Chapter 9 - Share Your Travels</title>
<link rel="stylesheet" href="css/styles.css" />
<script type="text/javascript" src="js/chapter09-project02.js">
document.getElementById("thumbnails").addEventListener("click", function(e) {
if(e.target && e.target.nodeName.toLowerCase == "img") {
// Taking the image src and converting it to medium image.
var srcValue = e.target.src.replace("small","medium");
// Taking title and storing it for later.
var titleValue = e.target.title;
document.getElementById("featured").src = srcValue;
document.getElementById("featured").title = titleValue;
document.getElementById("figcaption").innerHTML = titleValue;
}
});
document.getElementById("figcaption").addEventListener("onmouseover", function(e) {
var figcaption = e.target.style.opacity = .8;
});
document.getElementById("figcaption").addEventListener("onmouseout", function(e) {
var figcaption = e.target.style.opacity = 0;
});
</script>
</head>
<body>
<header>
<h2>Share Your Travels</h2>
<nav><img src="images/menu.png"></nav>
</header>
<main>
<figure id="featured">
<img src="images/medium/5855774224.jpg" title="Battle" />
<figcaption>Battle</figcaption>
</figure>
<div id="thumbnails">
<img src="images/small/5855774224.jpg" title="Battle" />
<img src="images/small/5856697109.jpg" title="Luneburg" />
<img src="images/small/6119130918.jpg" title="Bermuda" />
<img src="images/small/8711645510.jpg" title="Athens" />
<img src="images/small/9504449928.jpg" title="Florence" />
</div>
</main>
</body>
</html>
Is something like this what you were after?
var caption = document.getElementsByTagName("FIGCAPTION")[0];
var images = document.getElementsByTagName("IMG");
for (val of images) {
if(images[0] !== val){
val.addEventListener("click", function(e) {
caption.innerHTML = this.title;
images[0].title = this.title;
images[0].src = this.src.replace("small","medium");
});
}
}
img {
width: 100px;
height: 100px;
}
<!DOCTYPE html>
<html lang="en">
<head >
<meta charset="utf-8">
<title>Chapter 9 - Share Your Travels</title>
<link rel="stylesheet" href="css/styles.css" />
</head>
<body>
<header>
<h2>Share Your Travels</h2>
</header>
<main>
<figure id="featured">
<img src="https://cdn.pixabay.com/photo/2018/01/20/14/26/panorama-3094696__340.jpg" title="Battle" />
<figcaption>Battle</figcaption>
</figure>
<div id="thumbnails">
<img src="https://cdn.pixabay.com/photo/2018/05/15/09/23/raindrop-3402550__340.jpg" title="Battle" />
<img src="https://cdn.pixabay.com/photo/2018/04/23/14/23/giraffe-3344366__340.jpg" title="Luneburg" />
<img src="https://cdn.pixabay.com/photo/2018/04/20/18/19/grass-3336700__340.jpg" title="Bermuda" />
<img src="https://cdn.pixabay.com/photo/2018/05/12/12/48/mountain-crumpled-3393209__340.jpg" title="Athens" />
<img src="https://cdn.pixabay.com/photo/2018/04/24/19/14/hai-3347787__340.jpg" title="Florence" />
</div>
</main>
</body>
</html>

Uncaught TypeError: Cannot read property 'src' of undefined at displayFull

I am having trouble with this homework assignment and I have been stuck on it for a while. I am getting an error message saying
Uncaught TypeError: Cannot read property 'src' of undefined at
displayFull
Any idea to fix this?
The image folders and files are in the same folder, so the src is right.
HTML
<!DOCTYPE html>
<!-- Fig. 12.5: coverviewer.html -->
<!-- Dynamic styles used for animation. -->
<html>
<head>
<meta charset = "utf-8">
<title>Deitel Book Cover Viewer</title>
<link rel = "stylesheet" type = "text/css" href = "style.css">
<script src = "coverviewer.js"></script>
</head>
<body>
<div id = "mainimg">
<img id = "imgCover" src = "fullsize/jhtp.jpg" alt = "Full cover image">
</div>
<div id = "thumbs"> // I AM GETTING THE ERROR MESSAGE HERE FOR EACH IMAGE ON CLICK AT WITH THE ONCLICK ATTRIBUTE
<img src = "thumbs/jhtp.jpg" id = "jhtp" alt = "Java How to Program cover" onclick = "displayFull('jhtp');">
<img src = "thumbs/iw3htp.jpg" id = "iw3htp" alt = "Internet & World Wide Web How to Program cover" onclick = "displayFull('iw3htp');">
<img src = "thumbs/cpphtp.jpg" id = "cpphtp" alt = "C++ How to Program cover" onclick = "displayFull('cpphtp');">
<img src = "thumbs/jhtplov.jpg" id = "jhtplov" alt = "Java How to Program LOV cover" onclick = "displayFull('jhtplov');">
<img src = "thumbs/cpphtplov.jpg" id = "cpphtplov" alt = "C++ How to Program LOV cover" onclick = "displayFull('cpphtplov');">
<img src = "thumbs/vcsharphtp.jpg" id = "vcsharphtp" alt = "Visual C# How to Program cover" onclick = "displayFull('vcsharphtp');">
</div>
</body>
</html>
JAVASCRIPT
var fullpic = new Array(6);
//define each array element as an image
for (var i=0;i<fullpic.length;i++)
fullpic[i] = new Image(289,373);
//give the path to the src properties of the array elements
fullpic[0].src = "fullsize/jhtp.jpg";
fullpic[0].alt = "Java How to Program";
fullpic[1].src = "fullsize/iw3htp.jpg";
fullpic[2].src = "fullsize/cpphtp.jpg";
fullpic[3].src = "fullsize/jhtplov.jpg";
fullpic[4].src = "fullsize/cpphtplov.jpg";
fullpic[5].src = "fullsize/vcsharphtp.jpg";
/*function displayFull(title){
document.getElementById("imgCover").src="fullsize/"+title+".jpg";
}*/
function displayFull(i){
document.getElementById("imgCover").src=fullpic[i].src; // I AM GETTING THE ERROR MESSAGE HERE
}
To make it cleaner and readable, You can just do something like this as mentioned below. All you need is , have all the images under in your markup.
window.onload = function() {
document.querySelector("#thumbs").addEventListener("click", function(event) {
if (event.target.tagName.toLowerCase() === "img") {
var title = event.target.getAttribute("id"),
imgSrc = "fullsize/" + title + ".jpg",
imgElement = document.querySelector("#imgCover");
imgElement.src = imgSrc;
imgElement.alt = imgSrc;
}
});
}
<!DOCTYPE html>
<!-- Fig. 12.5: coverviewer.html -->
<!-- Dynamic styles used for animation. -->
<html>
<head>
<meta charset="utf-8">
<title>Deitel Book Cover Viewer</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="coverviewer.js"></script>
</head>
<body>
<div id="mainimg">
<img id="imgCover" src="fullsize/jhtp.jpg" alt="Full cover image">
</div>
<div id="thumbs">// I AM GETTING THE ERROR MESSAGE HERE FOR EACH IMAGE ON CLICK AT WITH THE ONCLICK ATTRIBUTE
<img src="thumbs/jhtp.jpg" id="jhtp" alt="Java How to Program cover">
<img src="thumbs/iw3htp.jpg" id="iw3htp" alt="Internet & World Wide Web How to Program cover">
<img src="thumbs/cpphtp.jpg" id="cpphtp" alt="C++ How to Program cover">
<img src="thumbs/jhtplov.jpg" id="jhtplov" alt="Java How to Program LOV cover">
<img src="thumbs/cpphtplov.jpg" id="cpphtplov" alt="C++ How to Program LOV cover">
<img src="thumbs/vcsharphtp.jpg" id="vcsharphtp" alt="Visual C# How to Program cover">
</div>
</body>
</html>

change thumbnail image for main image

do i have a syntax error? , could it be that im using notepad++?
nothing is happening when i click a thumbnail image
i am new to javascript so that may be the case.
my general idea was to create an event "changeimage" that onclick would change the source of my main image for my thumbnails image to make it bigger.
<head>
<script type="text/javascript">
function changeimage(event)
{
event = event;
var targetElement = event.target;
if (targetElement.tagName == "IMG")
{
document.getElementbyid("Main").src = targetelement.getattribute("src");
}
}
</script>
<meta charset="utf=8">
<style>
#Main
{
height:540px;
width:540px;
}
.imgthmb1
{
height:100px;
width:100px;
}
</style>
</head>
<body>
<img id="Main" src="img/rickroll1.jpg" </img>
<br />
<div id="thumbnail" onclick="changeimage(event)">
<img class="imgthmb1" src="img/rickroll1.jpg" />
<img class="imgthmb1" src="img/rickroll2.jpg" />
<img class="imgthmb1" src="img/rickroll3.jpg" />
<img class="imgthmb1" src="img/rickroll4.jpg" />
<img class="imgthmb1" src="img/rickroll5.jpg" />
</div>
</body>
</html>
Yes, you have a syntax error:
targetElement is spelled targetelement in the following line:
document.getElementbyid("Main").src = targetelement.getattribute("src");
Change it to the following:
document.getElementbyid("Main").src = targetElement.getattribute("src");
On a related note, the following line is extraneous and can be removed:
event = event;
Javascript is case-sensitive.
in place of
document.getElementbyid("Main").src = targetelement.getattribute("src");
write
document.getElementbyId("Main").src = targetElement.getAttribute("src");
Also your html markup has an error:
<img id="Main" src="img/rickroll1.jpg" </img>
should be
<img id="Main" src="img/rickroll1.jpg">

TypeError: 'undefined' is not an object (evaluating 'arrayImg[img].style')

I'm trying to write a javascript program which show an hidden image.
Here's the html part :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title> Exercice 3 </title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header>
</header>
<section>
<article>
<figure>
<img src="images/powsoundeffect.png" alt="image" />
<figcaption>
Image 1
</figcaption>
</figure>
<figure>
<img src="images/thwack_soundeffect.png" alt="image" />
<figcaption>
Image 2
</figcaption>
</figure>
<figure>
<img src="images/tophat.png" alt="image" />
<figcaption>
Image 3
</figcaption>
</figure>
<figure>
<img src="images/1311511622.png" alt="image" />
<figcaption>
Image 4
</figcaption>
</figure>
<ul>
<li>
Image 1
</li>
<li>
Image 2
</li>
<li>
Image 2
</li>
<li>
Image 2
</li>
</ul>
</article>
</section>
<script type="text/Javascript" src="script.js" >
</script>
</body>
</html>
The css :
figure {
width: 20%;
position: absolute;
visibility: hidden;
}
ul li {
border: 1px solid black;
width: 10%;
}
The script :
var arrayA = document.querySelectorAll('a');
var arrayImg = document.querySelectorAll('figure');
function show_Image(text, img) {
text.style.fontSize = '2em';
arrayImg[img].style.visibility = 'visible';
}
for (var i = 0, c = arrayA.length; i < c; i++) {
arrayA[i].addEventListener('mouseover', function(e) {
show_Image(e.target, i);
},
false);
}
When I try to run the script the following error occurs on safari :
TypeError: 'undefined' is not an object (evaluating 'arrayImg[img].style')
and on chromium :
Uncaught TypeError: Cannot read property 'style' of undefined
It seems that the arrayImg is not containing object with the querySelector and I don't see why ...
Anyone can help me on this ?
Thanks !
As Jonathan Lonowski described in its comment, the variable 'i' is increased after the eventListener has been bound - and the anonymous function will use the increased value.
You may go this way:
for (var i = 0, c = arrayImg.length; i < c; i++) {
arrayA[i].addEventListener('mouseover', (function(i,e) {
show_Image(e.target, i);
}).bind(this,i), false);
}
It binds the anonymous function to the scope the binding is executed from and 'i' as a first parameter, that won't change anymore.
Even if that isn't the cleanest way to achieve what you want to do, it's working in the way you want it to.

Categories

Resources