View Full Version : Javascript issue
tribalmp
10-09-2008, 10:54 AM
Hi,
How is possible to show an image without using document.write
document.write('<img src="image.jpg" border="0">')
Thanks for all the help provided
codeexploiter
10-09-2008, 11:16 AM
Here is a demo page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<style type="text/css">
</style>
</head>
<body>
<div id="imgcontainer"></div>
<script type="text/javascript">
function imgInsert(src,alt,border,width,height,container){
if(!container){
container = document.createElement('div');
document.body.appendChild(container);
}else if(typeof container === 'string'){
container = document.getElementById(container);
}
var img = document.createElement('img');
if(src){
img.src = src
}
if(alt){
img.alt = alt;
}
if(border){
img.border = border;
}
if(width){
img.width = width;
}
if(height){
img.border = height;
}
container.appendChild(img);
}
window.onload = function(){
imgInsert("image.jpg","this is a demo", 0,null,null,"imgcontainer");
}
</script>
</body>
</html>
The only required thing is an image whose name in image.jpg and it should store with this page (the folder in which you've saved this source).
tribalmp
10-09-2008, 02:47 PM
Hi,
Not working as expected, here i include the code in question.
<SCRIPT LANGUAGE="JavaScript">
function imageError() {
alert("This message will show if the image didnt load")
}
</script>
<img src="images/fail.jpg" border=0 onError="imageError(this)" onAbort="imageError(this)">
-----------------------------------------------
And i changed to this in hope to load a second image
-----------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
function imageError() {
document.write('<img src="image/ok.jpg" border="0">')
}
</script>
<img src="images/fail.jpg" border=0 onError="imageError(this)" onAbort="imageError(this)">
But, the problem is that the image loads in a new layer, cleanning all the text, css, etc
-----------------------------------------------
Any help is welcome, thanks
jscheuer1
10-09-2008, 03:54 PM
If you already have an image tag in your document:
<img src="whatever.jpg" id="theimage" alt="">
You can change it by:
document.getElementById('theimage').src = 'another.jpg';
codeexploiter
10-10-2008, 04:20 AM
If you use document.write() after your page loading then it will removes all the page content and insert the content you've inserted using document.write(). So whenever you use this method you should make sure that you are calling it before the page load completes otherwise the result would be different than you expects.
In the demo page I've provided I've done it using without employing the docunment.write(). It would be better if you explain what kind of functionality you required then only the forum users will be able to help you.
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.