0
    var projectWindow;
    function btnNew_Click() {
        var form = document.createElement("form");
        form.setAttribute("target", "projectWindow");
        form.setAttribute("method", "post");
        form.setAttribute("action", "MySite.aspx");

        document.body.appendChild(form);

        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("id", "hiddenValue");
        hiddenField.setAttribute("name", "ID");
        hiddenField.setAttribute("value", "/*get value from javascript*/");

        form.appendChild(hiddenField);

        //Below line I added to fix the new input text field that was visible
        document.getElementById("hiddenValue").style.visibility = 'hidden';

        form.submit();

        //The below line i added to give focus if another window is created
        // The below code does not work
        projectWindow.focus();

        //I also tried this:
        form.focus();
}

My problem in the above code that i obtained from the thread: Javascript Post on Form Submit open a new window

is that there is an input field that is shown at the client that I cant hide, except for the first time with the line I added (maybe this is about many values return from the method getElementbyId).

The second problem is that I want to set focus in the newly created or reloaded window, currently it is only working with the new window.

Community
  • 1
  • 1
Ahmad Hajou
  • 1,289
  • 5
  • 22
  • 39

2 Answers2

2

Try adding it to the onload of the new window:

projectWindow.onload = function(){projectWindow.focus()};

Didn't test this code, hope this helps! Oh! and to create a hidden input, do this:

var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");

Also, getElementById should not return many values, just one! If it does return more than one, there is something wrong.

Victor
  • 9,210
  • 3
  • 26
  • 39
  • the onload things is not working, how can i add the onload to the newly loaded window instead of the code at the opener page. – Ahmad Hajou Dec 21 '09 at 11:08
  • It appears that the problem that i had was related to the firefox security rules. This worked perfectly in Internet Explorer – Ahmad Hajou Dec 29 '09 at 12:58
0

To add on Victor's answer. The function btnNew_Click always create a new form element.

You either have to check for it's existence and not recreate or destroy it after submitting it.

o.k.w
  • 25,490
  • 6
  • 66
  • 63
  • Im already checking for the availability of the new window: form.setAttribute("target", "projectWindow"); can u tell me how if this is the intended thing, if not please let me know what can i do to set it right. – Ahmad Hajou Dec 21 '09 at 11:00
  • I'm refering to the form created by document.createElement, not the window. – o.k.w Dec 21 '09 at 11:13