Click to See Complete Forum and Search --> : Need help setting up a javacript code with XHTML


jalaln
October 31st, 2006, 10:09 AM
Hello all,

I want to insert a javascript code into my website (XHTML 1.0 Strict valid) that generates random images.

I found one.

I have to insert the first part between <head> and </head>

<script type="text/javascript">
var imagenumber = 5 ;
var randomnumber = Math.random() ;
var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;
images = new Array
images[1] = "images/1.jpg"
images[2] = "images/2.jpg"
images[3] = "images/3.jpg"
images[4] = "images/4.jpg"
images[5] = "images/5.jpg"
var image = images[rand1]
</script>



Then, the second part in the body section:


<script type="text/javascript">
document.write('<img src="' + image + '"alt="Image" width="228" height="145" />')
</script>


The script works fine but the page is invalid.


I've got this error while validating in w3 http://validator.w3.org



Error Line 70 column 91: document type does not allow element "img" here.

... Image" width="228" height="145" />')

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).


I've tried many ways but still don't get what I have to fix! Any reply will be highly appreciated.

Thanks,
Jalal

Dr. Script
October 31st, 2006, 02:50 PM
Have you tried something to the effect of:

document.write('<i'+'mg src="' + image + '"alt="Image" width="228" height="145" />')

PeejAvery
October 31st, 2006, 04:59 PM
Error Line 70 column 91: document type does not allow element "img" here.

... Image" width="228" height="145" />')
Since it would appear to be improperly placed, can you please give the code around it?

code?
November 4th, 2006, 06:39 PM
try using ' in javascript to represent that theres a " in html
without those, your code would look like this

<img id=myimg src=images/img1 class=image onclick=function()/>

instead of this

<img id="myimg" src="images/img1" class="image" onclick="function()"/>


see, even codeguru doesn't get the syntax right when there's no "

try this

document.write("<img src=' + image + ' alt='Image' width='228' height='145' />")


if that doesnt work, replace " with &, its the raw html code for ", and it is less likly for you to make "mistakes that'" + 'way"

PeejAvery
November 4th, 2006, 06:52 PM
document.write("<img src=' + image + ' alt='Image' width='228' height='145' />")

Sorry, code? but that is incorrect syntax. The original is correct.

EDIT: That will literally read the source as '+image+'.

code?
November 4th, 2006, 06:56 PM
oh ok

PeejAvery
November 4th, 2006, 07:01 PM
Since the original code that jalaln posted is syntactically correct, let's wait until he/she posts the rest of the code.