Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Simple JavaScript not working in IE7
Old 05-31-2009, 05:56 AM Simple JavaScript not working in IE7
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Im using this code to hide an element before it is loaded into the DOM, but IE just seems to ignore it!

Code:
var head = document.getElementsByTagName ('head');
head     = head[0];
var css  = document.createTextNode ('#coolText { display:none; }');
var obj  = document.createElement ('style');
obj.type = 'text/css';

obj.appendChild (css);
head.appendChild (obj);
Any ideas why it might not be working?
Thanks.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 05-31-2009, 10:48 AM Re: Simple JavaScript not working in IE7
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Hmmm I've managed to fix it by changing it to this:

Code:
document.write ('<style type="text/css">#coolText { display:none; }</style>');
Still no clues on why IE didn't like it though.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 06-01-2009, 09:08 AM Re: Simple JavaScript not working in IE7
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
I don't think IE allows you to append style elements to the head. They want you to use their StyleSheet object. Or it could be possible that IE just doesn't allow you to create text nodes to dictate style rules.

Just take the approach of using inline styles instead. Inline styles are universally compatible. The slightly older Opera and Safari don't even allow stylesheet changes.

If you need something to be hidden only if JavaScript is enabled, do something like this:

HTML Code:
<body><!--opening body tag-->
<script type="text/javascript">
document.body.className = (document.body.className)
   ? document.body.className + " enabled"
   : "enabled";
</script>
This adds a class of enabled to the BODY element, but won't remove any class that already is on the body. Then, in your regular stylesheet, you could specify a rule like this:

CSS
Code:
.enabled #coolText {display: none;}
Since the class is added before the rest of the HTML is parsed (the script is right after the opening BODY tag), the special text will be hidden automatically and will never "flicker".
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 06-01-2009 at 09:10 AM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to Simple JavaScript not working in IE7
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.51977 seconds with 12 queries