1

I am a javascript noob and I need help with this problem !

On a specific condition a function is called and when it is called , I want an image to be displayed ! How do I do that ? this is not working!

<script type="text/javascript">
function _enabled() {
document.write("<img border="0" src="http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some image.png" />");
}

var r =true;
</script>

This is not working , I get this error

Error: missing ) after argument list
Source File: file:///C:/Users/Gowtham/Desktop/new%20%202.html
Line: 5, Column: 33
Source Code:
document.write("<img border="0" src="http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/sjs.png" />"); 

5 Answers 5

4

The syntax error is that you're using double quotes for your document.write string literal, but you've also used a double quotes your HTML attributes:

document.write("<img border="0" src="http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some image.png" />");
//                          ^--- here and other places

That double quote ends the string literal, so you get a syntax error. You'd want to escape that with a backslash, or use JavaScript's handy feature where you can use single quotes around string literals.

But that alone isn't going to solve the problem. If you call document.write after the main parsing of the page is finished, you're going to completely erase the page and replace it with new content.

To display an image after the main parsing of the page is complete, you create a new img element via the DOM document.createElement function and then append it to the element in which you want it to appear. For instance, this code puts a new image at the end of the document:

function addTheImage() {
    var img = document.createElement('img');
    img.src = "http://....";
    document.body.appendChild(img);
}

To add to another element, rather than the end of body, you just need to get a reference to the element. There are various ways to do that, including document.getElementById to look it up by its id value, document.getElementsByTagName to look up all elements with a given tag, etc. Some browsers offer very rich methods like querySelectorAll that let you use CSS selectors, but not all do yet. (Many JavaScript libraries like jQuery, Prototype, YUI, Closure, or any of several others plug that gap for you, though, and offer other handy utility features, and features smoothing over browser inconsistencies and outright browser bugs.)

More about the dom:

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

2 Comments

I did know about document.createElement but it is a big task and I want the image in a particular postion only . That's for advanced guys not for noobs :)
You should start learning JavaScript. createElement thing is commonly used when generating contents in page using JavaScript.
1

Escape your document.write codes.

<script type="text/javascript">
function _enabled() {
document.write("<img border=\"0\" src=\"http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some%20image.png\" />");
}

var r =true;
</script>

2 Comments

it depends what other codes you have. This code piece will not clear your page & the function is not called as well.
actually the document.write is nested !
1

You need to escape your quote marks, or use single quote marks inside the string passed to document.write. For example "<img border=\"0\"... or <img border='0'...

At the moment, your string ends at the start of your first attribute.

Comments

1
<script type="text/javascript">
  function _enabled() {
    document.write("<img border='0' src='http://4.bp.blogspot.com/-    C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some image.png' />");
   }

  var r =true;
</script>

Note: Single quotes.

Tested works fine.

2 Comments

Note the double quote near "border" :-)
@cypher yes.. @Hack tested in chrome latest,FF4 , works smoothly
0

You either need to escape quotes or use single quotes for javascript and double for html (for example).

<script type="text/javascript">
    function _enabled() {
        document.write('<img border="0" src="http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some image.png" />');
    }

    var r =true;
</script>

Comments

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.