1

I have an html document that has 2 divs (left and right) and the doc loads with some js functions, one function named editme would usually load in the left div, but when the user expands the right nav I want this editme function to launch/open up a new window and do stuff, which means one of the following options would work if possible: 1) destroy the existing function and recreate a new function with the same name 2) change the js function on the fly

Which is possible and how?

Thanks.

Added the following based on suggestions:

edit=function(){} // works, however less value

edit=function(id) { 'window.open("editMe.cfm?taid="+id,"edit","left=260,top=20,height=410,width=590,resizable=1,scrollbars=1")' } // fails

SyntaxError: unterminated string literal

attempted to scape the double quotes like the following to no avail, 'window.open(\"editMe.cfm?taid=\"+id,\"edit\",\"left=260,top=20,height=410,width=590,resizable=1,scrollbars=1\")'

Am I doing something wrong?

fyi, this code is part of other code for the onclick event.

4
  • 4
    This question would be much easier to answer if you posted your current code, preferrably on jsfiddle along with a description of anything you have tried. Commented Apr 24, 2013 at 16:24
  • 1
    can you show us some code? its hard to tell what you're asking Commented Apr 24, 2013 at 16:25
  • 1
    I suspect you are using in-line script blocks. You don't need to delete and replace the function, that's completely against how functions are supposed to work. Call the same function but pass in an argument to tell which div you are calling it from. Commented Apr 24, 2013 at 16:25
  • 1
    Doesn't editme = function() { ... } work for you? Commented Apr 24, 2013 at 16:27

1 Answer 1

5

Functions are first-class objects in JavaScript. Simply reassign a new function to the variable you've already (implicitly) created...

function foo() {
    //do stuff
}

//now, reassign the var to do something else
foo = function() {
    //do other stuff
}

Not saying this is good practice... but to answer your question, yes it's possible.

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

1 Comment

Thank you all, and @adamb and user375487, I'll try it first thing tomorrow morning.

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.