0

I am using javascript to edit the contents of an element depending on the browser. It is a checkbox that in all modern browsers can be clicked to reveal extra content, but not in IE8 or earlier.

However, the element I am wishing to change is echo'd via php, and I can't get the content to change. The JS file does however, mean the rest of my page doesn't appear.

The element is in the first line of my code below - . The intention is to replace the content of this element with a simple link, meaning that users of IE8 and earlier who will not be able to use the checkbox to see the hidden content, will be able to view it on another page.

Here's some code

echo "<p id=\"browser\">";

echo "<div class=\"resultsContainer\">";
echo "<input type=\"checkbox\" name=\"show\" class=\"show\">
                <label for=\"show\">Recent Results</label>

                <article class=\"small\">

                <table class=\"results\">";



$query2=$database->query("SELECT team_name, team_score, 
opposition_score, opposition_name from results_a ORDER BY 
updated DESC LIMIT 0, 5");
$i=0;
while ($row2=$query2->fetch(PDO::FETCH_NUM)) {
echo ($i %2 == 0)? "<tr class=\"stripe\">" : "<tr class=\"nostripe\">";
printf ("<td>%s<td class=\"small\">%s<td class=\"small\">%s<td>%s</tr>", 
$row2[0], $row2[1], $row2[2], $row2[3]);
$i++;
}
echo "</table>";
echo "</article>";
echo "</div>";
echo "</p>";
echo "<br>";


?>
                <!--[if lt IE 9]>
<script src="oldBrowser.js"</script>
<![endif]--> 
<noscript>
<!--[if lt IE 9]>
<p>To view results you must either upgrade Internet Explorer to 
IE9 or 10, or enable ActiveX Controls on this page</p>
<![endif]-->
</noscript>

And the JS...

var browser = document.getElementById("browser");
browser.innerHTML = "<a href='results.php'>See results</a>";

Is it something to do with my code being in PHP?

4
  • 1
    You are missing a > closing the <script tag. Commented Feb 20, 2014 at 17:45
  • Good. Was not sure if it was only a copy paste error/typo or one in actual script. Also, if this is HTML5 your leading <p> tag will get closed when the <div> starts – in effect <p id="browser"> will be empty. That closing <p> tag is somewhat confusing if that is the intention. Recommend validator.w3.org Commented Feb 20, 2014 at 17:57
  • In HTML4 I'm not sure what would happen, browser dependent I guess, but spec is clear: w3.org/TR/html401/struct/text.html#h-9.3.1 "The P element represents a paragraph. It cannot contain block-level elements (including P itself)." Commented Feb 20, 2014 at 18:01
  • it is html5. That's great knowledge, thanks for that really helpful. Luckily once I knew the error I changed to a div anyway as it made more sense. Thanks again! Commented Feb 20, 2014 at 19:05

2 Answers 2

3
<script src="oldBrowser.js"</script>

should be

<script src="oldBrowser.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

0

You never closed your <p> tag:

echo "<p id=\"browser\"></p>";
                        ^^^^^---missing

so ALL of the html you echo after the <p> tag is contained within that <p>, and gets replaced when your browser.innerHTML code is executed.

5 Comments

He actually close it.
yes, but after all the table and whatnot. so when the js executes, it'll replace everything.
Depends on HTML version. In HTML5 it would get worse/better (all depends I guess) as the <p> would be closed when the div starts. Ref. paragraph
He want to replace all, but @user13500 is right, <p> will be closed by the div tag in HTML 5
I changed it to a div anyway as I was just experimenting with the <p> as the Div wasn't working, but it was all because of the missing > ! Thanks for all the answers.

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.