0

See following code

<div id="news-ticker">
<marquee id="news-marquee" scrollamount="3" onmouseover="stop()" onmouseout="start()" style="padding-top:2px; padding-bottom:2px;">
Latest News
</marquee>
</div>

<script type="text/javascript">

var currenturl = document.URL;

if ((currenturl.indexOf("&lang=nl") != -1) || (currenturl.indexOf(";lang=nl") != -1) || (currenturl.indexOf("&lang=nl#content") != -1))  {
document.getElementById("news-marquee").innerHTML = '<?php require_once("news_nl.php"); ?>';
}

else if ((currenturl.indexOf("&lang=en") != -1) || (currenturl.indexOf(";lang=en") != -1) || (currenturl.indexOf("&lang=en#content") != -1))  {
document.getElementById("news-marquee").innerHTML = '<?php require_once("news_en.php"); ?>';
}

else if ((currenturl.indexOf("&lang=fr") != -1) || (currenturl.indexOf(";lang=fr") != -1) || (currenturl.indexOf("&lang=fr#content") != -1)) {
document.getElementById("news-marquee").innerHTML = '<?php require_once("news_fr.php"); ?>';
}

else {
document.getElementById("news-marquee").innerHTML = '<?php require_once("news_nl.php"); ?>';
}

</script>

On a HTML page, I have a div that contains a marquee to display the latest news on the webpage. The contents that need to be displayed in the marquee are taken from a php file. Here's what's happening:

When the page loads, I first see "Latest News" appearing in the marquee. That is to say: It displays shortly before the javascript gets executed. After that, the news-marquee shows the correct content: If I'm on the Dutch (&lang=nl) part of the website, the Dutch news items are shown, the French items are shown on the French site etc. So that works.

But some pages don't have the "&lang=xx" trail on the URL, so in those cases I have the else condition which displays the Dutch news items by default. This however, does not get executed. The news-marquee stays blank. Not even "Latest News" is shown anymore.

Firebug doesn't throw any errors at me, so I have no idea what could be going on here.

2
  • how about placing a break point in firebug and seeing what execution path your app is taking? Commented Jul 27, 2012 at 11:45
  • I'd put something in your code to make sure that the program is running the else that you are expecting. Even something as simple as alert("else 1 called"); Commented Jul 27, 2012 at 11:47

2 Answers 2

2

Your issue is your use of require_once instead of include. require_once only allows the file to be included once.

I would recommend doing the check in PHP and only giving the client one of the files to reduce bandwidth usage too.

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

1 Comment

That did it, thanks! I can accept your answer in 8 minutes :)
0

Don't use innerHTML period. It's a proprietary Microsoft JScript method that does not correctly parse code it inserts in to the DOM. In translation when you try to interact with code you have "inserted" via innerHTML it may or may not be visible to the browser.

Stick to W3C standard methods such as appendChild, importNode, insertBefore and replaceChild.

If you need to convert between text and code use new DOMParser() to parse text-to-code and new XMLSerializer() to parse code-to-text.

Also you should stick to using single quotes in PHP as double quotes are used to interpret variables as text which requires more resources. I am presuming that the includes are being executed on the server and that you're not actually expecting those to be executed at the client.

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.