Kang
Kang

Reputation: 1

Why href is not working in javascript closure

I meet two problem about this code :

  1. I can't link to another website when I call "window.location.href = 'www.google.com';" in javascript closure.

  2. This code is not working in IE browser (the submit button is disapper)

Below is the code:

HTML partment : (A hidden form that controlled by 'Create folder' button)

<a href="#" class="mylink" title="Create Folder" onClick="modalPopup(0);">Create folder</a>
    <!-- modal content -->
    <div id="modal-content">
        <div id="modal-title"></div>
        <div id="modal-text"></div>
        <form name="input" action="" method="post">
            <div id="modal-field"></div>
            <input type="hidden" name="ID" id="ID">
            <input type="hidden" name="submitButton" id="submitButton">
        </form>
    </div>

Javascript partment : ( show the form and handle the submit event )

function deleteNode( elementId ) 
{
    var label=document.getElementById( elementId ); 
    while ( label.hasChildNodes() ) 
    { 
        label.removeChild( label.lastChild ); 
    }
}

function sendRequest( type, param )
{
    if ( type == "createFolder" )
    { 
        alert('create folder : ' + param ); 
        **window.location.href = "www.google.com";  //  just for test, but not working !**
    }
    return false;
}

function modalPopup( fileID ) 
{
    deleteNode("modal-title");
    deleteNode("modal-text");
    deleteNode("modal-field");

    var titleObj = document.createElement("h3");
    titleObj.innerText = "Create Folder";
    titleObj.textContent = "Create Folder";
    document.getElementById("modal-title").appendChild(titleObj);

    var textObj = document.createElement("a");
    textObj.innerText = "Enter new folder name";
    textObj.textContent = "Enter new folder name";
    document.getElementById("modal-text").appendChild(textObj);

    var fieldObj = document.createElement("input");
    fieldObj.setAttribute("type","input");
    fieldObj.setAttribute("name","createFolder");
    fieldObj.setAttribute("value","New Folder");
    fieldObj.setAttribute("id","modal-field");
    document.getElementById("modal-field").appendChild(fieldObj);

    // assign text field value
    document.getElementsByName('submitButton')[0].type = "submit";
    document.getElementsByName('submitButton')[0].value = "Create";
    **document.getElementsByName('submitButton')[0].onclick = function() { return sendRequest( "createFolder", fieldObj.value ); };**
    document.getElementsByName('ID')[0].value = fileID;             
}

Upvotes: 0

Views: 450

Answers (1)

Bill Effin Murray
Bill Effin Murray

Reputation: 436

jbabey's right:

<input type="hidden" name="submitButton" id="submitButton">

should be:

<input type="submit" name="submitButton" id="submitButton">

Put a

return false;

at the end of the function you have bound to the submit button

and try:

window.location.href = "http://www.google.com";

Upvotes: 1

Related Questions