Javascript函数GetChatURL未定义

Javascript Function GetChatURL is not Defined

本文关键字:未定义 GetChatURL 函数 Javascript      更新时间:2023-09-26

所以这些定义问题通常与语法有关,但我没有看到我的语法错误。我的问题是,为什么我的函数没有定义,当我有它定义的onload?

我试图改变这个链接的onclick事件打开一个URL与一些值我没有,直到运行时。下面的所有代码都包含在body标签中。

HTML

<a id ="chaturl" style="position: fixed; top: 55%; right: 0; height: auto;" href="http://someurl" target="_blank" onclick="GetChatURL()"><img alt="Chat" src="~/Content/images/check.png" border="0"></a>

这里是一个misc RenderSection位于

之间
@RenderSection("scripts", required: false)

这是JS

<script type="text/javascript">
window.onload = function () {
    document.getElementById("chaturl").onclick = function GetChatURL() {
        var firstName = @(System.Web.HttpUtility.UrlEncode((User as OneStop.Core.Entities.Principal).User.FirstName));
        var lastName = @(System.Web.HttpUtility.UrlEncode((User as OneStop.Core.Entities.Principal).User.LastName));
        var email = @(System.Web.HttpUtility.UrlEncode((User as OneStop.Core.Entities.Principal).User.Email));
        var phone = @(System.Web.HttpUtility.UrlEncode((User as OneStop.Core.Entities.Principal).User.PhoneNumber));
        return "window.open(http://someurl/chat.aspx?firstname=" + firstName + "&lastname=" + lastName + "&email=" + email + "&phonenumber=" + phone + ", 'Chat', 'toolbar=no,location=no,directories=no,menubar=no,status=no,scrollbars=no,resizable=yes,replace=no');this.newWindow.focus();this.newWindow.opener=window;return false;";
    }
}
</script>

首先,函数不应该返回 URL,而应该打开 URL。

此外,不需要将函数称为GetChatURL。把

document.getElementById("chaturl").onclick = function GetChatURL() {

document.getElementById("chaturl").onclick = function () {

或者,您可以单独定义该函数,然后需要将其更改为:

document.getElementById("chaturl").onclick = GetChatURL;

,然后定义函数:

function GetChatURL() {
    var firstName = @(System.Web.HttpUtility.UrlEncode((User as OneStop.Core.Entities.Principal).User.FirstName));
    var lastName = @(System.Web.HttpUtility.UrlEncode((User as OneStop.Core.Entities.Principal).User.LastName));
    var email = @(System.Web.HttpUtility.UrlEncode((User as OneStop.Core.Entities.Principal).User.Email));
    var phone = @(System.Web.HttpUtility.UrlEncode((User as OneStop.Core.Entities.Principal).User.PhoneNumber));
    window.open("<url>")
}