在 C# 文件中调用 javascript 函数

本文关键字:javascript 函数 调用 文件 | 更新日期: 2023-09-27 18:30:40

我在页面加载方法中调用 C# 文件中的 JavaScript 函数

<script type="text/javascript">
    function googleMaps(aLocations, aTitles, aSummary) {
        $(document).ready(function () {
            $('#test').GoogleMap(aLocations, aTitles, aSummary, {
                type: 3,
                zoom: 9
            });
        });
    }
</script>

</script>

而 JavaScript 代码是

   function googleMaps(aLocations, aTitles, aSummary) {
        $(document).ready(function () {
            $('#test').GoogleMap(aLocations, aTitles, aSummary, {
                type: 3,
                zoom: 9
            });
        });
    }

我收到此错误

caught SyntaxError: Unexpected token function

错误在以下行

WebForm_InitCallback();var _spFormDigestRefreshInterval = 1440000;googleMaps('["keswick,cumbria,uk","grasmere,cumbria,uk","ambleside,cumbria,uk"]','["Keswick","Grasmere","Ambleside"]','["'u003cdiv class='"ExternalClass1721CEC0617F42C887D068EA11963662'"'u003e'u003cfont class='"ms-rteThemeForeColor-2-0'" face='"Consolas'" size='"2'"'u003e'u003cfont face='"Consolas'" size='"2'"'u003e'u003cfont face='"Consolas'" size='"2'"'u003e'u003cp'u003e'u0026lt;h3'u0026gt;Keswick'u0026lt;/h3'u0026gt;'u0026lt;p'u0026gt;Keswick is a market town and civil parish within the Borough of Allerdale in Cumbria, England. It had a population of 4,984, according to the 2001 census.'u0026lt;/p'u0026gt;'u003c/p'u003e'u003c/font'u003e'u003c/font'u003e'u003c/font'u003e'u003c/div'u003e","'u003cdiv class='"ExternalClass4AFD1E67B5374D86A656C26E69EEE124'"'u003e'u003cfont class='"ms-rteThemeForeColor-2-0'" face='"Consolas'" size='"2'"'u003e'u003cfont face='"Consolas'" size='"2'"'u003e'u003cfont face='"Consolas'" size='"2'"'u003e'u003cp'u003e'u0026lt;h3'u0026gt;Grasmere'u0026lt;/h3'u0026gt;'u0026lt;p'u0026gt;Grasmere is a village, and popular tourist destination, in the centre of the English Lake District. It takes its name from the adjacent lake of Grasmere, and is associated with the Lake Poets.'u0026lt;/p'u0026gt;'u003c/p'u003e'u003c/font'u003e'u003c/font'u003e'u003c/font'u003e'u003c/div'u003e","'u003cdiv class='"ExternalClass8A0E058969E04AF59AF3B8FDACAB4576'"'u003e'u003cfont class='"ms-rteThemeForeColor-2-0'" face='"Consolas'" size='"2'"'u003e'u003cfont face='"Consolas'" size='"2'"'u003e'u003cfont face='"Consolas'" size='"2'"'u003e'u003cp'u003e'u0026lt;h3'u0026gt;Ambleside'u0026lt;/h3'u0026gt;'u0026lt;p'u0026gt;Ambleside is a town in Cumbria, in North West England. Historically within the county of Westmorland, it is situated at the head of Windermere, Englands largest lake. The town is within the Lake District National Park.'u0026lt;/p'u0026gt;'u003c/p'u003e'u003c/font'u003e'u003c/font'u003e'u003c/font'u003e'u003c/div'u003e"]')function loadMDN2() { EnsureScript('MDN.js', typeof(loadFilterFn), null); }

在 C# 文件中调用 javascript 函数

        var serializer = new JavaScriptSerializer();
        var serializedResult1 = serializer.Serialize(getLocations());
        var serializedResult2 = serializer.Serialize(getTitles());
        var serializedResult3 = serializer.Serialize(getSummary());
        Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "googleMaps('" + serializedResult1 + "','" + serializedResult2 + "','" + serializedResult3 + "');", true);

我将您的JS复制并粘贴到JSLint中并得到相同的错误,但是当我将函数的开头移动到JSLint中框的最顶部时,它消失了。它说它期望在第 1 列运行。似乎表明可能值得将该功能提升到最顶部,并将其作为标签之后的第一件事 - 即删除它前面的空格。试一试JSLint,你就会明白我的意思