Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CSS - can you hide javascript in DIV?

Posted on 2012-03-31
5
Medium Priority
?
232 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:Gurvinder Pal Singh
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:
Gurvinder Pal Singh earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

722 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