views:

363

answers:

0

So I've got a script that I'd like to add to a page using a ScriptManager, but the script gets added multiple times. The script I'm trying to add plus the call to register it (in the C# code behind) are as follows:

    string buttons = @"var overflowEls = getElementsByClass(""descriptionBox"");

    for (var i = 0; i < overflowEls.length; i++)
    {
        if (checkOverflow(overflowEls[i]))
        {
            var html = overflowEls[i].innerHTML;
            overflowEls[i].innerHTML = overflowEls[i].innerHTML = ""<div class=\""videoFeedDescriptionButtons\"" onclick=\""expand(this);\"">+</div><br/><div onclick=\""collapse(this);\"" class=\""videoFeedDescriptionButtons\"">-</div>"" + html;        

        }
    }";

    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "key1", buttons, true);

This is taking place in the Page_Load function.

This works just fine when the page loads, I assume because it only exists once. However, once a partial postback is performed, a duplicate of this script is added to the head section of the html page. The code actually continues to work in Firefox, I assume because the script it outputs to the head is formatted correctly. Not the case in other browsers, they output something like this:

<script type="text/javascript">

var overflowEls = getElementsByClass("descriptionBox"); for (var i = 0; i < overflowEls.length; i++) { if (checkOverflow(overflowEls[i])) { var html = overflowEls[i].innerHTML; overflowEls[i].innerHTML = overflowEls[i].innerHTML = "

<div class="\"onclick="\">+</div>

<div onclick="\"collapse(this);\""class="\"videoFeedDescriptionButtons\"">-</div>
"" + html;}}"

</script>

This causes it to break. If you are curious exactly how the brokenness exhibits itself, see my previous question related to this: http://stackoverflow.com/questions/1575558/problems-with-innerhtml-using-javascript/1579242 . Also, I am adding other unrelated scripts to the same element (UpdatePanel1) as this script, I don't know if that matters or not.

So I'm confused on a number of counts. First off, I thought that ScriptManager.RegisterStartupScript added the script to the end of the page, only. It does in fact add it to the end of the page where I expected when the page first loads, but subsequently the script is always added to the head. Secondly, I thought the ScriptManager checks to see if the script is a duplicate, and if it is, does not add it. The key here is hardcoded, so that's not changing, and there's only one updatepanel (and definitely only one called UpdatePanel1) so how is it not recognizing that the script already exists?

And finally, perhaps the strangest part of all of this is that the scripts in the head don't show up when viewing the page source. Yup, the only one thats visible in the page source is the one that should be added, at the end of the page and properly formatted. The only way I was able to spot the duplicates in the head is via something like Firebug in Firefox, or the Inspect Element option in Chrome.

If you need any other clarification or anything just ask, thanks.