Jin
Jin

Reputation:

Running HTML through Javascript

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>

Upvotes: 0

Views: 1492

Answers (4)

Aziz
Aziz

Reputation: 20765

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

Upvotes: 0

LeJeune
LeJeune

Reputation: 950

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(" ");

Upvotes: 0

Mark
Mark

Reputation: 1376

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

Upvotes: 1

Nick Berardi
Nick Berardi

Reputation: 54894

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.

Upvotes: 2

Related Questions