This question already has answers here:
jQuery/JavaScript to replace broken images
(32 answers)
Closed 6 years ago.
I need to be able to detect if an image is broken and replace with a default image if the image link is broken. I know i could do this with an image proxy, but was hoping to do it on the fly with javascript.
I believe it's the onerror event of the img element. onerror=function(){} though i've never used it.
As any event the onerror will propagate upwards on the DOM, so you could make a generic handler for this type of errors.
<script type="text/javascript">
jQuery(document).bind('error', function(event) {
console.log(event.target.src);
});
</script>
You can use <img onerror='doWhateverFunction()' etc etc
http://msdn.microsoft.com/en-us/library/cc197053(v=VS.85).aspx
Example of code:
<script language='javascript'>
function defaultImage(img)
{
img.onerror = "";
img.src = 'default.gif';
}
</script>
<img alt="My Image" src="someimage.gif" onerror="defaultImage(this);" />
Related
This question already has answers here:
Difference between object.src and object.getAttribute('src')
(3 answers)
Closed 2 years ago.
I'm having a very confusing time with this very simple onClick function in Javascript/html. I know there are many questions on this but couldn't quite find an answer given what my script does (Or doesn't in this case). It should be simple but for some reason the logic is just not working as I expect it too.
<h1>The onclick Event</h1>
<img id="onoff" onclick="changeImage();" src="images/Off Button.jpg" width="245" height="238">
<p>Click on button to turn "on"</p>
<script>
function changeImage() {
var image = document.getElementById("onoff");
if (image.src.match("Off Button.jpg")){
image.src = "images/On Button.jpg";
}
else{
image.src="images/Off Button.jpg";
}
}
</script>
So as you can see this should take the original "off button" image and swap it to "on button" when it is clicked provided it is showing the "Off Button.jpg".
However, it does nothing, using the Chrome developer tools I can see the script doesn't even fire. But when I make these changes:
if (image.src.match("Off Button.jpg")){
image.src = "images/Off Button.jpg";
}
else{
image.src="images/On Button.jpg";
It now fires and changes the Button to "On Button" but does not fire again to change it back. For me, this makes no sense logically but I might just be missing something really obvious. I know this is pretty basic but any help or explanation would be appreciated.
I did some changes to the code and now it's working.
I used the relative path with ./ to img src and mostly you should avoid use spaces to name images or anything else - spaces always cause problems ;)
<h1>The onclick Event</h1>
<!-- relative path and no space in name on src -->
<img
id="onoff"
onclick="changeImage();"
src="./images/off-button.jpg"
width="245"
height="238"
/>
<p>Click on button to turn "on"</p>
<script>
function changeImage() {
var image = document.getElementById('onoff');
if (image.src.match('off-button.jpg')) { /* no space in name */
image.src = './images/on-button.jpg'; /* relative path and no space
in name */
} else {
image.src = './images/off-button.jpg'; /* relative path and no space
in name */
}
}
</script>
Note - Remember to rename the images inside the images folder as well
This question already has answers here:
Adding background image to button using CSS
(2 answers)
Closed 7 years ago.
im currently trying to style my javascript quiz by replacing the "submit answer" button with a image so instead of clicking the button to submit user can click the image. The button once clicked executes a function which checks the answer.
Here is the original button code ive used, any ideas how to replace with image>
"<button onclick='checkAnswer()'>Submit Answer</button>";
many thanks
You can just add your img as an <img> tag, and an an onClick to that object:
img = document.createElement("img");
img.src = "https://www.google.com/images/srpr/logo11w.png";
img.onclick = function (){
alert("clicked!");
};
document.body.appendChild(img);
Hi in your Html you can just place the image and in your script you can place the function that you want to execute.An example from your code.
<img id="myimagebutton" src="image.png" alt="Submit Answer" onclick="checkAnswer()" />
inside your javascript:
function checkAnswer(){
var mybutton = document.getElementById("myimagebutton");
var answer = document.getElementById("answerbox").value or .innerHTML;
// the element that contains the answer ex answerbox depending on the type
//execute somelogic
}
If you want some label under the image "button" search for this html5 future <figcaption>
This question already has answers here:
Can I use javascript to dynamically change a video's source?
(10 answers)
changing source on html5 video tag
(19 answers)
Closed 9 years ago.
I have an html5 video streaming and I need to change it source* after some click action. So what i'm doing works, but only in html, i can see source change but it no changing on my display, can you help me? What is wrong? *The source is appending frome xml file.
HTML
<video autoplay loop width="960" height="540" id="video">
<source src="video/movie_01.mp4" id="tv_main_channel">
</video>
JS
btn.on('click', function(){
var tv_main_channel = $('#tv_main_channel'),
d_program_source_mp4 = $(program_self).find("program_source_mp4").text();
tv_main_channel.attr('src', d_program_source_mp4);
}
also i try it with append but it still not work
var video_block = $('#video');
video_block.empty();
video_block.append(
'<source src="'+ d_program_source_mp4 +'">'
);
Thx for help.
See this fiddle: http://jsfiddle.net/qGbzb/2/
To dynamically load videos you need to run
var video_block = $('#video');
video_block.load();
Then you should also see a change in the display too, and not only in the html.
It's simple, just do this:
btn.on("click", function(){
var src = "new_video_src.mp4";
$("#video").find("#tv_main_channel").attr("src", src)
})
You can use like this in click event
var src = "new_video_src.mp4";
$("#tv_main_channel").src=src;
This is the actual method for video tag elements
This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 8 years ago.
<html>
<head>
<title>Test javascript</title>
<script type="text/javascript">
var e = document.getElementById("db_info");
e.innerHTML='Found you';
</script>
</head>
<body>
<div id="content">
<div id="tables">
</div>
<div id="db_info">
</div>
</div>
</body>
</html>
If I use alert(e); it turns up null.... and obviously I don't get any "found you" on screen. What am I doing wrong?
The problem is that you are trying to access the element before it exists. You need to wait for the page to be fully loaded. A possible approach is to use the onload handler:
window.onload = function () {
var e = document.getElementById("db_info");
e.innerHTML='Found you';
};
Most common JavaScript libraries provide a DOM-ready event, though. This is better, since window.onload waits for all images, too. You do not need that in most cases.
Another approach is to place the script tag right before your closing </body>-tag, since everything in front of it is loaded at the time of execution, then.
How will the browser know when to run the code inside script tag? So, to make the code run after the window is loaded completely,
window.onload = doStuff;
function doStuff() {
var e = document.getElementById("db_info");
e.innerHTML='Found you';
}
The other alternative is to keep your <script...</script> just before the closing </body> tag.
Script is called before element exists.
You should try one of the following:
wrap code into a function and use a body onload event to call it.
put script at the end of document
use defer attribute into script tag declaration
The script is performed before the DOM of the body is built. Put it all into a function and call it from the onload of the body-element.
Run the code either in onload event, either just before you close body tag.
You try to find an element wich is not there at the moment you do it.
Is it possible to call a JavaScript function from the IMG SRC tag to get an image url?
Like this:
<IMG SRC="GetImage()" />
<script language="javascript">
function GetImage() {return "imageName/imagePath.jpg"}
</script>
This is using .NET 2.0.
Nope. It's not possible, at least not in all browsers. You can do something like this instead:
<img src="blank.png" id="image" alt="just nothing">
<script type="text/javascript">
document.getElementById('image').src = "yourpicture.png";
</script>
Your favourite JavaScript framework will provide nicer ways :)
If you're in the mood for hacks, this works as well.
<img src='blah' onerror="this.src='url to image'">
Is it possible to call a JavaScript function from the IMG SRC tag to get an image url?
Do you mean doing something like the following?
<img src="javascript:GetImage()" />
Unfortunately, no - you can't do that. However, you can do the following hack:
function getImageUrl(img) {
var imageSrc = "imageName/imagePath.jpg";
if(img.src != imageSrc) { // don't get stuck in an endless loop
img.src = imageSrc;
}
}
Then, have the following html:
<img src="http://yourdomain.com/images/empty.gif" onload="getImageUrl(this)" />
The onload event will only fire if you have an actual image set to the src attribute - if you don't set that attribute or set it to an empty string or something similar, you will get no love. Set it to a single pixel transparent gif or something similar.
Anyway, this hack works, but depending on what you are really trying to accomplish, this may not be the best solution. I don't know why you would want to do this, but maybe you have a good reason (that you would like to share with us!).
You cannot do it inline the image #src, but you should be able to call it from an inline script block immediately following your image:
<img src="" id="myImage"/>
<script type="text/javascript">
document.getElementById("myImage").src = GetImage();
</script>
you could dynamically feed the image by calling an aspx page in the SRC.
Ex;
<img src="provideImage.aspx?someparameter=x" />
On the page side, you`ll need to put the image in the response and change the content type for an image.
The only "problem" is that your images won't be indexed a you better put some cache on that provider page or you'll ravage the server.
Are you looking for this.src ?`
<img src='images/test.jpg' onmouseover="alert(this.src);">
Since you're using .NET, you could add the runat="server" attribute and set the src in your codebehind.
You might be able to do it on the server side. Alternately you could attach an onload event to swap the image src out. I guess the question then becomes, why would you have to use Javascript in the first pace?
I've had to do something like this before, and IIRC the trick winds up being that you can't change an src attribute of an image that's part of the DOM tree. So your best bet is to write your HTML skeleton without the image and 1)create an onLoad function that generates a new img element with document.createElement, 2) set the src attribute with setAttribute(), and 3) attach it to your DOM tree.
OnLoad event of image called again and again do some thing like this
how about this?
var imgsBlocks = new Array( '/1.png', '/2.png', '/3.png');
function getImageUrl(elemid) {
var ind = document.getElementById(elemid).selectedIndex;
document.getElementById("get_img").src=imgsBlocks[ind];
}
it's not work?
<img src="'+imgsBlocks[2]+'" id="get_img"/>
You may try this way also
const myImage = new Image(200, 200);
myImage.src = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABA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lTjUljUhSbpQqfRjHLKMcE44QWz8fWz7vRlPFylnlKUqbbnOU/Jk5Rjt+ypPHAD1UZSlGLlHLJxi5RxTcZNbY4rjwPsAAAAAAAAAAAAH/2Q==';
document.body.appendChild(myImage);
No. The Img's SRC attribute is not an event, therefore the inline JS will never fire.
OnLoad event of image called again and again do some thing like this