Solved

CSS - can you hide javascript in DIV?

Posted on 2012-03-31
5
202 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

"I want to put my photos online, but I don't want them stolen.  What settings should I use?" When You Put Photos Online First and foremost, any digital file published on the WWW can be copied, stored, modified, retransmitted, etc.  Remember Naps…
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
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 tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

821 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