Solved

CSS - can you hide javascript in DIV?

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Creating a CSS block that only applies to printing By default, all of your CSS applies to every possible view of your page - whether on screen, printed, landscape, touch-screen, or whatever.  You can, however, add CSS that only applies under certai…
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 style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
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…

738 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