Jump to content

Help needed! unable to change image size by javascript


peacehunter
 Share

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'/>");

Edited by peacehunter
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";

Edited by Deirdre's Dad
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" />

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...