Replace function don't work on dynamic page

in Script development
Subscribe to Replace function don't work on dynamic page 7 posts, 4 voices

jonas18z User

I doing:

unsafeWindow.check_key function (e) {

I can get it to work on a own page simple page that's calls the same function.

But on the real page grecmonkey don't replace the function.

Sorry but I can't post the page it's an internal system

Jefferson Scher Scriptwright

If check_key is created by another script, your change might be too early. You could try adding it after a delay using window.setTimeout.

Couchy Scriptwright

Well, you forgot the equals sign for one thing. Aside from that, the preferred way of replacing functions on the page is to tack on a script element instead of using unsafeWindow.

function injectScript(scriptContent)
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");

var replacementFunction = function(){check_key = function(e){alert("replaced!");};};

jonas18z User

Thanks for all help I tried with delay, and the tips from Couchy. The equals was just a typo here.

Maybe I doing some totally wrong

Here is an example for the problem, I can't replace check_key but I can replace the function preLoad.

html+ javascript

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script language="JavaScript1.2">

ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false

var oldkeyhandler;

function nullkeyhandler(e)

if (ns4) document.captureEvents(Event.KEYPRESS);

	oldkeyhandler = document.onkeypress; 
	oldkeyhandler = nullkeyhandler; 
document.onkeypress = check_key;

  function check_key(e) {
//   whichASC = (ns4) ? e.which : event.keyCode;
    alert(whichASC = e.which );

  function preLoad() { 



<body onLoad="javascript:preLoad();">

	<input class="tid" type="text" size="1" name="falt" onFocus="setCurFoc('falt')">



Iskaral_Pust User

Why is it, that your way of replacing a function works but deleting the offending script tag from the original document does not?

jonas18z User

Sorry but really I don't understand what you mean? But the script I was posted above is part from the original site, that's an intranet.

And I removed/modified some function and code that I can't post and.

Here is the example from my code with the problem function.

I can't replace the function check_key with either unsafeWindow or Couchys method.

Maybe greacemonkey can't replace this kind of function or what do I do wrong?

Iskaral_Pust User

I just found this splendid userscript example which uses the @run-at directive to replace any script on a website. It's quite magical!