SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Disable javascript in particular div through coding

    How do i disable javascript in particluar div through coding i guess javascipt

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you provide an example?

  3. #3
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure.
    I want to disable javascript for the div in red

    Code:
    <Body>
    JavaScript
    My contents
    
    <div id="cont">
    JavaScript
    </div>
    
    </body>

    Quote Originally Posted by jimfraser View Post
    Can you provide an example?

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want to disable <script> tags that are in that div? Or you want to prevent other scripts from acting on that div?

  5. #5
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want prevent other scripts from acting on that div


    Quote Originally Posted by jimfraser View Post
    You want to disable <script> tags that are in that div? Or you want to prevent other scripts from acting on that div?

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rewrite your javascript to not do anything with that div.

    Otherwise, you'd have to go through the div and systematically break certain attributes depending on what the script in question does, and how.

    Again, if you can be more specific (show us the script and the page in question), you might get a better answer

  7. #7
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could generate the DIV using javascript after the various events have been attached (by the script which you have no control over), - this way the events won't be attached because there will be no DIV there to attach them to.... Does that make sense? ... I haven't tried this - so it might not work.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  8. #8
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Following on from what I said, have a look at this:

    Code HTML4Strict:
    <body>
    <div id="div">
    STUFF
    </div>
     
    <script type="text/javascript">
    var div = document.getElementById('div');
    var content = div.innerHTML;
    var parent = div.parentNode;
    parent.removeChild(div);
    function reCreateElement() {
     parent.appendChild(div);
    }
    setTimeout('reCreateElement()',100);
     
    // Below is the stuff we don't want to run!
    document.getElementById('div').onclick = function() {
     alert('CLICKED!');
    }
    </script>
    </body>

    The "onclick" event will not work because the element does not exist at the time it is attached.

    I know using "innerHTML" (not W3C DOM) is not the right way to do it but this is just an example.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The trouble is that people learn by example.

    With the above example, the div will be pulled out, then put back at the bottom of its parent, which may not be what is wanted, so we'll record the next sibling (if available) and use that to place things back where they were.

    The innerHTML isn't required at all. All you need is the reference to the element itself and things will be fine.

    Code javascript:
    var div = document.getElementById('div');
    var nextSibling = div.nextSibling;
    var parent = div.parentNode;
    parent.removeChild(div);
    function reCreateElement() {
    	if (nextSibling) {
    		parent.insertBefore(div, nextSibling);
    	} else {
    		parent.appendChild(div);
    	}
    }
    setTimeout(reCreateElement, 100);
     
    // Below is the stuff we don't want to run!
    document.getElementById('div').onclick = function() {
     alert('CLICKED!');
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Paul,

    Thanks for improving my code...

    Anyway, do you think 100ms is long enough for the timeout?

    @rashidr - I am quite interested in what you want this for... Is it to prevent userscripts?
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  11. #11
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting to see all these answers without having the question cleared in the first place

    Keep up the good spirit!


  12. #12
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all Thanks to all for your answers. Now; back to the point, Can we improve this by disallowing every script calling js file in that div.


    Yes, It is to prevent userscript.

    Quote Originally Posted by JimmyP View Post
    Hello Paul,
    @rashidr - I am quite interested in what you want this for... Is it to prevent userscripts?

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The given technique works only for scripts that activate on page load.
    When scripts activate based on an event of some kind, that is less likely to have any solution.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One more question/request. If i want to allow certain fuctions in that div. How can i do that?

  15. #15
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rashidr View Post
    How do i disable javascript in particluar div through coding i guess javascipt
    http://www.catb.org/~esr/faqs/smart-...tml#bespecific

    You should probably read the whole article though.

  16. #16
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rashidr View Post
    One more question/request. If i want to allow certain fuctions in that div. How can i do that?
    Allow only certain or all?

    I believe that Paul already answered all your questions.


  17. #17
    SitePoint Guru rashidr's Avatar
    Join Date
    Jun 2004
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only certain,

    Quote Originally Posted by Pepejeria View Post
    Allow only certain or all?

    I believe that Paul already answered all your questions.


  18. #18
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to better explain what the problem is. It will otherwise end up like the beginning of the thread were people tried to guess what you meant.

  19. #19
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use the code posted above, but want to allow certain other functions to act on the div then you'll have to give those other functions a timeout of 100ms also.

    And yeh it would be good to have some more info.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •