0

I am making a bookmarklet and need to change some code inside a page. For example, after page loaded it creates a function which is used 'onclick'. I need to replace a code inside a variable of this function. For example here is a function:

function openNewWindow(){
    newWindow = window.open('http://www.example.org','params','width=200,height=200,resizable=0');

And I need to change this code into this:

function openNewWindow(){
    newWindow = window.open('http://www.example.org','params','_blank');

How can I do it, taking in account, that the function is loaded by ajax?

8
  • the function is loaded by ajax - are you loading a function declaration content by ajax? Commented Dec 4, 2016 at 21:35
  • ajax.php loads a part of code after it calculates a data. Commented Dec 4, 2016 at 21:43
  • if that content is generated by backend side, then make replacement on backend and let front-end get a prepared content Commented Dec 4, 2016 at 21:47
  • But if I have only frontend access? It's a bookmarklet that clicks on popup in a new tab, instead of new window. Commented Dec 4, 2016 at 21:49
  • let's comprehend things as they are: are you working with some text and just trying to replace one substring to another ? Commented Dec 4, 2016 at 22:05

1 Answer 1

1

Functions can be overwritting by being asigned a new refrence, if you only have access to the front end of the code after the fact. You can replace openNewWindow with a new function;

openNewWindow = function () {
    newWindow = window.open('http://www.example.org','params','_blank'); 
}

However replacing functions that come from third parties are not recomended in a lot of cases because it can produce unexpected results.

Sign up to request clarification or add additional context in comments.

4 Comments

But if I need to reaplce only a part of function's code, because the rest of the code might be random.
You can compose a new function to call your code and then the original functions code. Only replacing part of the code is not reccomended to keep doing something the safe way (avoiding function.toString() and eval).
But I can use var ev = window[fnstring]; instead of eval?
Unsure how that would solve your issue. That would access a global object with the value of fnstring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.