views:

62

answers:

2
    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: http://stackoverflow.com/questions/178964/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.

+2  A: 

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
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
It appears that the problem that i had was related to the firefox security rules. This worked perfectly in Internet Explorer
Ahmad Hajou
A: 

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
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
I'm refering to the form created by document.createElement, not the window.
o.k.w