Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide span (or div) until javascript completes

Tags:

javascript

I'm using the Unit PNG Fix (http://labs.unitinteractive.com/unitpngfix.php) to make a transparent png work in IE. It's being used on the background image of my logo container.

So I have my html, which contains these relative items. In the head:

<!--[if lt IE 7]>
        <script src="/assets/script_unitpngfix.js" type="text/javascript"></script>
<![endif]-->

Then in the body:

<a href="index.html" style="display:block;"><span id="Logo"></span></a>

Since, the logo loads with a gray box around it before the Unit PNG Fix works its magic, how can I just hide the entire span until after the javascript is executed, so the user never sees the gray box?

Thank you.

like image 311
Beau Avatar asked Jul 05 '10 17:07

Beau


3 Answers

Instead of your current span put this:

<span id='Logo' style="display:none"></span>

And then attach this function to the body onload event (<body onload = "showMySpan()">):

function showMySpan() {
  var mySpan = document.getElementById('Logo');
  mySpan.style.display = "";
}
like image 113
draganstankovic Avatar answered Oct 20 '22 07:10

draganstankovic


You could use something like jQuery and its ready function, which is specifically designed to wait for the DOM to be loaded.

The HTML would use display:none to start with :

<span id="Logo" style="display:none;"></span>

Then, add a script like this (assuming you've linked to jQuery too):

<script>
  $(document).ready(function () {
    $("#Logo").show();
  });
</script>
like image 38
Bruno Avatar answered Oct 20 '22 07:10

Bruno


Keep the span hidden by default:

<span id="Logo" style="display:none;"></span>

Then add a piece of JavaScript to show it later:

document.getElementById('Logo').style.display = 'inline';

The best way to ensure (at least in IE) that this piece of code is executed after the PNG Fix is to put it in a separate JS file and load it as a deferred script at the end of your page:

<script type="text/javascript" src="show_span.js" defer="defer"></script>

More details on deferred scripts: JavaScript: Defer Execution

like image 29
casablanca Avatar answered Oct 20 '22 06:10

casablanca