views:

145

answers:

4

I'm trying to run the body of a website through a javascript search I created. It works fine, except there are no spaces in between words; as well as the fact html in this xcase the image does not appear/show. Any solutions?

Therefore i need help getting the image to show. Then getting the text as seen to show on the browser with spaces.

This can all be found in the srctxt variable. Which is where i think the problem is.

Please, note i have only fairly basic knowledge of Javascript.

<body>
    <script type = "text/javascript">
        var keyword;
        var srctxt;
        var srctxtarray;

        keyword = "archery";
        srctxt = "hellow blah blah blah archery <img src =\"megan_fox.jpg\">hello test archery";

        srctxtarray= srctxt.split(" ");
        for (var i=0;i<srctxtarray.length;i++){
            if(srctxtarray[i] != keyword){
                document.write(srctxtarray[i]);
            }
            else{
                document.write("<b class=\"red\">");
                document.write(srctxtarray[i] );
                document.write("</b>");
            }
        }
    </script>
</body>
+2  A: 

Please edit your question, and make sure you are clear what you are asking for. Are you trying to get the image to show? Is there a problem getting the text from the DOM? How are you getting the text from the DOM?

Your biggest problem I can see right off the bat is that you are using document.write. You should use innerHtml to set the DOM text and elements.

Nick Berardi
Yes i am trying to get teh image to show. Also there is no problem getting the text. Asthe whole html body will be written in the srctxt variable.
You need to use the innerHTML property of what ever DOM object you are adding these to to get the img to show.
Nick Berardi
@Jin: Building off this answer, you may want to store the text in a text variable and then set body.innerHTML to the text at the very end. This way, the browser will render the tags. On a side note, you still need to put the spaces back into the page since split is stripping them out.
regex
ahh, i see. However i have never used innerHTML ill look it up thanks for the info.
+1  A: 

You are not outputting a space. You strip out the spaces when you srctxt.split and never do anything to put them back.

Mark
Thank you, this has helped as i havent noticed this. Any ideas on how i can re enter the spaces?
document.write(" ") after your if...else?
rvarcher
A: 

You are splitting the string around " " (spaces) but you dont write the spaces back.

After document.write(srctxtarray[i] ) you need to add

 document.write(" ");
LeJeune
A: 

The problem is that you are removing spaces (using split) and you do not put them back when you write the words. The image is not shown because the space between img and src is removed, and the tag looks like <imgsrc.

To put spaces, use:

document.write(srctxtarray[i] + " "); // will add spaces

But I strongly recommend that you use what Nick Berardi suggested

Aziz
Thanks for the suggestion however it only adds the spaces, after the word Archery rather than all words.
use that line in both conditions (the "if" and "else" parts), not only in the "if" part.
Aziz