简体   繁体   中英

Calling javascript function in C# File

I am calling JavaScript function in C# file in Page Load method

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



</script>

and JavaScript code is

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

I am getting this Error

caught SyntaxError: Unexpected token function

Error is on following line

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); }
        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);

I copied and pasted your JS into JSLint and got the same error, but when I moved the start of the function to the very top of the box in JSLint it went away. It says it expects function at column 1. Seems to suggest it might be worth bringing that function up to the very top and making it the first thing after your tag - ie remove the white space preceding it. Have a go in JSLint and you'll see what I mean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM