Solved

CSS - can you hide javascript in DIV?

Posted on 2012-03-31
5
190 Views
Last Modified: 2012-04-01
Is it possible to hide or stop the javscript from executing using css?.


<html>
<head>
<style type="text/css">
.mobile_hide{
 	display: none;	
}
</style>
	

</head>
<div class="mobile_hide">
<script type="text/javascript">			
   alert("hello");
</script>
</div>
<body>
Is is possible to hide the javascript alert with CSS?
</body>
</html>

Open in new window

0
Comment
Question by:sabecs
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37790899
Nope, it is not possible to unload or hide javascript using either css or javascript.

but you can try removing them it

var allScripts = document.getElementsByTagName("script");

for ( var counter = 0; counter < allScripts.length; counter++ )
{
    var script = allScripts[ counter ];
    var scriptparent = script.parentNode;
   
    if (  scriptparent.nodeName == "div"
            && scriptparent.getAttribute("class").indexOf() != -1 )
    {
        scriptparent.removeChild ( script  );
    }
}
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37793662
interesting question... and interesting answer!
Notice however, this answer is going to remove all scripts inside a <div> of any class.
0
 

Author Comment

by:sabecs
ID: 37794122
Thanks for your comments, is it possible to adjust the code above to remove just divs with class "mobile_hide" ?
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37794144
I guess this will work (haven't test it):


    if (  scriptparent.nodeName == "div"
            && scriptparent.className == 'mobile_hide' )
    {
        scriptparent.removeChild ( script  );
    }
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 37794689
@sabecs
In my reply, last if condition needs to be changed a bit

   if (  scriptparent.nodeName == "div"
            && scriptparent.getAttribute("class").indexOf("mobile_hide") != -1 )
    {
        scriptparent.removeChild ( script  );
    }
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When applying CSS to your HTML, there are many different ways to select which element(s) the CSS applies to.  Some of these selectors are more commonly known and used than others - Here are the more common ones: #X - Matches an ID of X .X - Matche…
When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now