Solved

CSS - can you hide javascript in DIV?

Posted on 2012-03-31
5
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
Suggested Courses

627 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