Forum Moderators: open

Message Too Old, No Replies

image src attribute in jscript

         

mickers

7:52 am on Feb 17, 2006 (gmt 0)

10+ Year Member



Hi All,
I have this really simple code to change the src attribute of an image on this page and it errors out with the jscript error:
"document.getElementById(...) is null or not an object"

This is all of the code on the page:
<html>
<head>
<title>Details</title>

<script language="JavaScript" type="text/javascript">

document.getElementById("asd").src = window.opener.document.all.picName.innerText;

</script>

</head>

<body>
<img src="" alt="" border="0" name="asd" id="asd">
</body>
</html>

That's it!
For the life of me I can't figure out why the image object isn't being recognized.

Note that the value of window.opener.document.all.picName.innerText is "images/big/largebarn.jpg", and that image exists in the correct folder.

Can anyone help here?

Thanks in advance.

mickers

8:04 am on Feb 17, 2006 (gmt 0)

10+ Year Member



BTW, I've also tried all of the following and they all result in the same error. The image just isn't being recognized:

document.all.asd.src = window.opener.document.all.picName.innerText;

document.images.asd.src = window.opener.document.all.picName.innerText;

var asd = new image();
document.getElementById("asd").src = window.opener.document.all.picName.innerText;

document.getElementById("asd").src = window.opener.document.all.picName.innerText;

isit = "asd"
document.getElementById(isit).setAttribute( 'src', window.opener.document.all.picName.innerText );

document.getElementById("asd").src = window.opener.document.all.picName.innerText;

window.document.getElementById("asd").src = window.opener.document.all.picName.innerText;

Bernard Marx

9:10 am on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the time you are trying to reference the image element, it doesn't yet exist - the HTML for it is below the script block.

1) Put the script block somewhere below both the image and the element, #picName.
//or//
2) Execute when the page has loaded.

onload = function()
{
// do your thing
}

BTW

Use getElementById not document.all
innerText won't work outside IE. Use innerHTML - although it too is non-standard.

document.getElementById("asd").src = window.opener.document.all.picName.innerText;

You are mixing referencing methods. Go for the modern, standards:

document.getElementById("asd").src = window.opener.document.getElementById("picName").innerText;

Bernard Marx

11:40 am on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BM:innerText won't work outside IE.

Sorry. You did specify JScript after all.

mickers

4:24 pm on Feb 17, 2006 (gmt 0)

10+ Year Member



Excellent!
That was the problem.

Amazing how referencing something that doesn't exist causes problems.

I very much appreciate it.