2

I have a function openWindow(word) that gets called when a button is clicked. This function opens a new window, prints the word and shows a button called do. The openWindow function works up until the onclick in its tag is supposed to find the do function. My problem is that onclick never seems to find my do function. What do I do?

My do function:

 function do() {
   alert("Life is what you make of it");
 }

My openWindow function:

 function openWindow(word) {
      myWindow.document.write(word);
      myWindow.document.write('<body>'); 
      myWindow.document.write('<input type="button" value="Do" onclick="do()">');
      myWindow.document.write('</body>');
     }      
  }

My html code to open window:

<body>
   <input type="button" value="Submit" onclick="openWindow()">
</body>
1
  • 'do' is a reserved keyword in JS. How is that you are not getting error? I tried in jsfiddle. I get unexpected token error. Commented Apr 4, 2013 at 18:53

3 Answers 3

1

do() does not exist in the new popup window. I suggest using this approach:

myWindow.document.write('<body>'); 
myWindow.document.write('<input type="button" value="Do" onclick="do()">');
myWindow.document.write('</body>');

// Define "Do" in the context of the new window
// Note: I have changed "do" to "Do" because "do" is a reserved word in JS
myWindow.Do = Do;

Here is a working fiddle (make sure are not blocking popups).

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

Comments

0

The do function is not part of the new window you have created, so it cannot be found.

You need to include it, either through a linked JavaScript file, or inline.

Comments

0

Your new window doesn't have the function definition to know what to do when "do()" is called.

If you simply want an alert with a button:

myWindow.document.write('<input type="button" value="Do" onclick="alert('Life is what you make of it')";

Otherwise, James Hill has the way to go.

Comments

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.