Jump to content

Help needed! unable to change image size by javascript


peacehunter

Recommended Posts

Recently I am working with javascript. But I am quite new in javascript. I am trying to change image size by detecting screen resolution . Screen resolution detection work properly , but image size does not change. I can not understand that where is my fault. Please help me. My codes are below...

var width = screen.width;var height= screen.height;document.write("<img src= 'index_1024_768.jpg' width= 'width' height= 'height'/>");

Link to comment
Share on other sites

All you're doing is printing the words 'width' and 'height' as strings, not the values of the variables. You need to take the variables out of the quotation. That will mean adding quotation marks and some plus signs to assemble a single string from bits and pieces. As in:width = 100;height = 200;txt1 = "hello ";txt2 = " bye ";str = txt1 + width + txt2 + height;// "hello 100 bye 200";

Link to comment
Share on other sites

you separate html/text content from JavaScript by inserting html/text between quotes then insert js variables using + symbol, example:document.write("<img src='myimage.jpg' width='"+width+"' height='"+height+"' />");remember to include single quote within the double quotes "...width='"+width+"' height=...."a better option might be to assign width height to class instead (place in between <head></head>)document.write("<style type='text/css'>");document.write(".screensize{height:"+height+"px; width:"+width+"px;}");document.write("</style>");then you only have to assign class ref to image<img src="myimage.jpg" class="screensize" />

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...