Pushkar Acharya
Pushkar Acharya

Reputation: 3

Javascript images not being displayed in Firefox and Chrome

I am new to javascript and trying to display an image in a new window. The code is -

<script type="text/javascript">
    function myfunc(){
        var new_window = window.open();
        var doc = new_window.document;
        var image = document.createElement('img');
        image.setAttribute('src','imagepath');
        image.setAttribute('alt',"image");
        doc.appendChild(image);
    }
</script>

This image is not being displayed in FireFox and Chrome while 'alt' text is displayed. While in IE nothing is being displayed. Kindly help.

Upvotes: 0

Views: 1118

Answers (3)

Mayur Gupta
Mayur Gupta

Reputation: 780

what i found is that dosent works nor newer versions ...

try this

var img = new Image(1,1); //width, height values are optional parameter acc to your requirments

img.src = 'your image url';

hope this will work

it worked for me in chrome not tested in firefox but i think it will work in firefox aswell.

Upvotes: 0

Piyush Arya
Piyush Arya

Reputation: 169

Hope this helps !!

Js Fiddle

<button>Working !!</button>

$('button').off('click').on('click', function(){
        var new_window = window.open();
        var doc = new_window.document.body;
        var image = document.createElement('img');
        doc.appendChild(image);        
       $(doc).find('img').attr({
        src: "http://profile.ak.fbcdn.net/hprofile-ak-ash4/276993_573451729347425_460197233_q.jpg",
             title: "Image" 
});

});

Upvotes: 0

Jivings
Jivings

Reputation: 23262

You can't insert a node into the document, you need to specify the body:

var doc = new_window.document.body;

It works fine for me with that correction.

Upvotes: 2

Related Questions