0

I can't figure out exactly how to make this work. I am new to PHP by the way.

Here is what I current have (zerkms's solution), and it still isn't working for some strange reason:

here is a link to the page on the server:

http://tinyurl.com/kd3gynk

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<?php



$srcmsg = 'http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg';
?>
<script type="text/javascript">
//<![CDATA[
//

var msr = "<?php echo $srcmsg; ?>";

window.onload = document.getElementsByTagName('img').src= msr;
//]]>
</script>
</head>
<body>
<img src="#" alt="Picture of the world" height="42" width="42" />
</body>
</html>
17
  • yes. I used firebug. it doesn't say anything except the usual error which is : The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol. Commented Jun 10, 2013 at 21:39
  • if you are asking about CDATA, that is just what you can use to make sure that all data inside the CDATA will be ignored by XML parser. just a style choice really. Commented Jun 10, 2013 at 21:43
  • No, I'm asking about the opening tag \<script .... Commented Jun 10, 2013 at 21:44
  • 1
    I removed the old part of the post Commented Jun 10, 2013 at 21:59
  • 1
    @user1066524 Most probably. I don't have enough experience with JSON, but enough to say yes. I suggest you ask another question with what you mentioned to me earlier, outlining your actual plan. I'm sure you'll get a good response if it's well outlined. Commented Jun 11, 2013 at 0:11

1 Answer 1

1

This has nothing to do with the PHP part.

This is not working purely because you are attempting to change an image that doesn't exist yet.

Either move your script to the end of the <body> (right before the </body> tag), or use window.onload = function() { /* your code here */ }, or implement some kind of deferring system.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried window.onload = function(){ document.getElementsByTagName('img').src= msr;}; and it still doesn't work.
That would be because getElementsByTagName returns elementS. Plural. It'd be better to put an ID on it and use getElementById, but if you can't do that then try getElementsByTagName('img')[0].
this the error I get when I try this: TypeError: document.getElementsByTagName('img')[0] is undefined
Did you combine it with one of the three solution I gave you in the answer?
Yes, I tried combining it with your solutions. Wouldn't you only use the [0] if it was a child element within the element? I thought [0] refers to the index of the first child in Javascript--this has no children. But thanks for your help Kolink.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.