Basically, I have an iframe which loads on every page on my website. Inside this is a header with a navigation bar coded using <ul>
and <li>
tags. eg.
<div id="header">
<ul>
<li><a id="BtnHome" href="/home">Home</a></li>
<li><a id="BtnServices" href="/services">Services</a></li>
<li><a id="BtnProducts" href="/products">Products</a></li>
<li><a id="BtnAbout" href="/about">About Us</a></li>
<li><a id="BtnBlank" href="#">Something</a></li>
</ul>
</div>
I need to change the background colour of an <a>
attribute when the header is loaded into its respective page. The closest I've got so far is by using this..
On the parent page:
<script type="text/javascript">
var URL = document.URL;
URL = URL.toUpperCase();
</script>
and in the iframe:
<script type="text/javascript">
if (URL.indexOf("/HOME") != -1) {document.getElementById("BtnHome").style.backgroundColor="#222";}
if (URL.indexOf("/SERVICES") != -1) {document.getElementById("BtnServices").style.backgroundColor="#222";}
if (URL.indexOf("/PRODUCTS") != -1) {document.getElementById("BtnProducts").style.backgroundColor="#222";}
if (URL.indexOf("/ABOUT") != -1) {document.getElementById("BtnAbout").style.backgroundColor="#222";}
</script>
The code in the iframe successfully changes the colour of the <a>
attribute if the variable 'URL' is matched up and the parent page sucessfully stores document.URL
into the 'URL' variable but it seems like the variable isn't accessible by the iframe.
I'm sure there is a good way of doing this but I've run out of ideas
It's worth noting that I'm trying to keep clutter to a minimum on all the parent pages (hence the use of iframes in the first place)
Thank you!