Jump to content

img src in document.write


phpgoal

Recommended Posts

Hello,

 

I have the below code. When myFunction is called, I want to see the slide show start but it does not. Document.write is correct?

 

Please help.

   <script type="text/javascript">

 if (datestr == 'September 2020')
 {var image1=new Image()
image1.src="car1.jpg"
var image2=new Image()
image2.src="car3.jpg"
myFunction(datestr);
}
 
 
function 
myFunction(a)
{
document.getElementById("demo").innerHTML=a;
 
document.write("<img src="car1.jpg" name="slide" width="435" height="500" 
/> ");

}
 
</script>
 

  if (datestr == 'September 2020') 
 
{var image1=new Image()
image1.src="car1.jpg"
var image2=new Image()
image2.src="car3.jpg"myFunction(datestr);
 
}
 
function 
myFunction(a){document.getElementById("demo").innerHTML=a;
document.write("<img src="car1.jpg" name="slide" width="435" height="500" 
/> ");}
 
Link to comment
https://forums.phpfreaks.com/topic/279121-img-src-in-documentwrite/
Share on other sites

You are ending your document.write too early.

 

document.write("<img src=" /* here your parsing stops the document.write method */ ...

 

Use single quotes for the img src or escape them with a backslash.

 

document.write("<img src=\"...\" ...") || document.write("<img src='...' ...");

I have tested in html it works. Not in JS.

 

works if i do in html as below:

<tr> <td colspan=6 align='center' ><img src="cars1.jpg" 
name="slide" width="435" height="500" /></td></tr>

Does not work if i write in JS as below:

document.write("<img 
src=\"car1.jpg\" name=\"slide\" /> ");

Below does the slide:
 

 
<script>
<!--
//variable that will increment through the images
var step=1
function slideit(){
//if browser does not support the image object, exit.
if (!document.images)
return
document.images.slide.src=eval("image"+step+".src")
if (step<2)
step++
else
step=1
//call function "slideit()" every 2.5 seconds
setTimeout("slideit()",2500)
}
slideit()
//-->
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.