0

After converting an ASP.NET webform (v3.5) to use a master page, I started getting a strange Javascript error.

The content page has a block of javascript. Here it is:

 <script type="text/javascript">

    var attCount = 0;

    function CreateAttachmentControl() {
        var newAtt = document.createElement('span');
        var newAttName = 'area' + attCount;
        newAtt.setAttribute('id', newAttName);
        newAtt.setAttribute('name', newAttName);

        var newInput = document.createElement('input');
        var newInputName = 'att' + attCount;
        newInput.setAttribute('type', 'file');
        newInput.setAttribute('id', newInputName);
        newInput.setAttribute('name', newInputName);

        if (newInput.addEventListener) {
            newInput.addEventListener("onchange", CreateAttachmentControl, false);
        } else if (newInput.attachEvent) {
            newInput.attachEvent("onchange", CreateAttachmentControl);
        } else {
            newInput.onchange = CreateAttachmentControl;
        }

        var newRemove = document.createElement('a');
        newRemove.setAttribute('href', 'javascript:RemoveAttachmentControl("' + attCount + '")');
        newRemove.setAttribute('title', 'Remove this attachment');
        newRemove.innerHTML = 'X';
        newAtt.appendChild(newInput);
        newAtt.appendChild(document.createTextNode(' '));
        newAtt.appendChild(newRemove);
        newAtt.appendChild(document.createElement('br'));
        attArea.appendChild(newAtt); // error here

        attCount++;
    }

    function RemoveAttachmentControl(n) {

        // get input element
        var input = document.getElementById('att' + n);

        // if the input is blank dont delete
        if (input.value != '' && input.value != null) {
            var att = document.getElementById('area' + n);
            attArea.removeChild(att);
        }
    }
</script>

The error is: 'attArea' is undefined

But, I know that it is not, because just below my block of javascript is this:

...<td align="left" colspan="2" style="height: 13px" id="attArea" name="attArea"></td>...

This working perfectly before I converted the webform into content page with a master page. Is there some known Javascript + Masterpage issues?

Thanks

2 Answers 2

3

In the code sample you provided, attArea is undefined. The first reference to it you are calling attArea.appendChild(). Is it declared somewhere higher in the source that you didn't provide?

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

1 Comment

Ok, I moved the call to that CreateAttachmentControl method to the very bottom of the page. What I was doing before I switch to masterpages was calling the method on the body onload event, which I don't have in a MP content page. Thanks for helping my confusion!
0

I think you are missing:

var attArea = document.getElementById('attArea');
attArea.appendChild(newAtt); // no more error!

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.