5

I've this government site page and using developer console I want to remove the onload event attached to the body tag by entering this code in the console:

var script = document.createElement("script");
script.src = "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
document.head.appendChild(script);

//wait for jquery library to load
setTimeout(function(){ 
$("body").unbind("onload");

$("body").get(0).onload = null;

 }, 3000);

But that does not work.

What is the way to remove this handler from attached javascript code? Since I'll need to remove it using an attache JS code in Google Chrome Extension.

I've not yet tried this code in Google Chrome Extension. First I want to make it work using developer console.

user5858
  • 1,082
  • 4
  • 39
  • 79
  • 8
    what is the purpose of removing onload from the body in the dev tools? surely the onload event has already happened by the time you get access to the html in the dev tools? – Toni Leigh Jun 04 '15 at 14:49
  • @Toni I want to stop it redirecting to advert and instead go to my ad link in the Chrome Extension. – user5858 Jun 04 '15 at 14:53
  • 2
    So why not just remove the onload handler in the source, this seems like a really weird question, and an excuse to get us to visit your page filled with ads ? – adeneo Jun 04 '15 at 14:54
  • @Patrick the problem with removeEventListner is that it also requires some function to be passed. What should I pass for function? – user5858 Jun 04 '15 at 14:55
  • 1
    @user5858, nevermind didnt see that you were talking about the onload attribute of the body tag. Just do `document.body.onload = null`, and not in a timeout, using a timeout will give time for the onload event to fire – Patrick Evans Jun 04 '15 at 14:57
  • on load event is already ran when you enter the developer tool, what you want to do is to unblind any event that created by the body load event – aahhaa Jun 04 '15 at 14:57
  • @adeneo that's a government site, I'm getting no profit if you visit it. – user5858 Jun 04 '15 at 15:24
  • @Patrick document.body.onload = null does not work and page still redirects. – user5858 Jun 04 '15 at 15:24
  • 1
    might make sense to update your question to explain you are making a chrome extension to do this and not running it in the dev tool – atmd Jun 04 '15 at 15:29
  • 1
    why not clear all timeouts ? like this http://stackoverflow.com/questions/8860188/is-there-a-way-to-clear-all-time-outs ? In fact this worked for me from the dev console. Also read [THIS](http://trak.in/tags/business/2015/03/02/irctc-amazon-affiliate-ecommerce/) before you think of making money from a govt. website. – Dhiraj Jun 04 '15 at 16:23
  • I'm sorry, but what's your point in unbinding `onload` after the page is loaded? – Xan Jun 04 '15 at 16:55

1 Answers1

1

The following JQuery will remove the onload function from your body tag:

$('body').off('load');

See more about off here

Toni Leigh
  • 4,830
  • 3
  • 22
  • 36