tags:

views:

72

answers:

2

hi every body I'm trying to open an image in a new window ,using javascript, by clicking on a button, then set a class to the image, using this code:

    OpenWindow=window.open("", "newwin","height=100,width=1000"); 
    OpenWindow.document.write("<img id='img1' src='webfonts.jpg'>");
    OpenWindow.document.getElementById("img1").className = "larger";
    OpenWindow.document.close();

and this is the internal style I have included in my code in a style tag: .larger{ width:10px; } and in the body tag 1:I have include my image source by an img tag 2:make a button tag with an onclick=larger() attribute

but unfortunately I have a problem with last sentence, the class is not assigned at all to the image. I have tried to use classname also instead of setttribute but it generates the same result. any ideas? thx

A: 

On a side note, you will also want to include in this new page some css otherwise this class="larger" will not do anything.

OpenWindow.document.write('<html><link href="css/style.css"/><body>');
OpenWindow.document.write('<img src="{source URL}" id="img1" class="larger" />');
OpenWindow.document.write('</body></html>');
Chris
first of all thanks a lot for your fast response, but i have included the class as internal style in my page this is the full code:<style>.larger{ width:1000px; }</style><script type="text/javascript"> function larger() { OpenWindow.document.write('<html><body>');OpenWindow.document.write('<img src="webfonts.jpg" id="img1" class="larger" />');OpenWindow.document.write('</body></html>'); }</script><body><img id="img1" src="webfonts.jpg" border="0" /><button onclick="larger()">larger</button></body>
loll
<body><img id="img1" src="webfonts.jpg" border="0" /><button onclick="larger()">larger</button></body>
loll
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>resize image </title><style>.larger{ width:1000px; }</style><script type="text/javascript"> function larger() { OpenWindow.document.write('<html><body>');OpenWindow.document.write('<img src="webfonts.jpg" id="img1" class="larger" />');OpenWindow.document.write('</body></html>'); }</script></head><body><img id="img1" src="webfonts.jpg" border="0" /><button onclick="larger()">larger</button></body></html>
loll
As others have noted, please update the code in the original question - trying to assemble 12 separate code fragments is very difficult
scunliffe
I have updated my question, but unfortunately it isnt allowed to add this code as new user :and this my code in body tag:<body><img id="img1" src="webfonts.jpg" border="0" /><button onclick="larger()">larger</button></body>
loll
<body><img id="img1" src="webfonts.jpg" border="0" /><button onclick="larger()">larger</button></body>
loll
the image is opened successfully in the new window but the class is not working at all
loll
+2  A: 

First, the code you wrote is wrong. There's an apostrophe too much behind img.

Second, you didn't assign an ID to your img element, so you can't access it with document.getElementById.

Use something like

var docHead = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'+
    'http://www.w3.org/TR/html4/strict.dtd"&gt;'+
    '<html><head><title>Test page</title>'+
    '<style type="text/css">'+
    '  .larger { width: 10px }'+
    '</style></head><body>';
var docFoot = '</body></html>';

OpenWindow=window.open("", "newwin", "height=100,width=1000");
OpenWindow.document.write(docHead);
OpenWindow.document.write('<img id="img1" src="webfonts.jpg">');
OpenWindow.document.write(docFoot);
OpenWindow.document.close();
OpenWindow.document.getElementById('img1').className = "larger";
Marcel Korpel
plz reread the code I have assigned an id to my image:<img id="img1" src="webfonts.jpg" border="0" />in the body
loll
I have tried your code
loll
but it doesn't work also, listen it seems to be better if i send u an attached file, but I am a new user, could u plz tell me how to do this.
loll
I use fire fox 3.6.8 on windows platform
loll
could u plz paste me ur code
loll
@loll – This *is* my code; I just surrounded it with `<script>` tags in an HTML file and ran it. The window appeared and checking with Firebug revealed that the class name was indeed set to ‘larger’.
Marcel Korpel
and the image has been re sized to have a width=1000px? this my problem the class has no effect on the image size.
loll
but if u rried this document.getElementById("img1").className = "larger"; (with out OpenWindow) the image will be resized successfully in the main page.
loll
document.getElementById("img1").className = "larger";
loll
@loll – That's a completely different problem. Did you embed a style sheet in the new window? The code above doesn't. I'll rework it a bit.
Marcel Korpel
yes I have embedded an internal style using this line code:<style>.larger{ width:10px; }</style>
loll
@loll – It's not in your code above. Perhaps you embedded it to the page that opens the new window, but you have to embed it there, too. See my revised answer.
Marcel Korpel
aha ok I'll try it immediately
loll
YES That's it, t finally works successfully thaaaaaaaaaaanks alot
loll
@scunliffe – Thanks for editing, but in HTML 4.01 (the OP didn't specify the version and variant he's using) the end tag of the `img` element is [forbidden](http://www.w3.org/TR/html401/struct/objects.html#h-13.2). ;-)
Marcel Korpel