未捕获的ReferenceError:未定义sendcard

时间:2015-10-19 07:19:41

标签: javascript html5

好的,所以我一直得到这个错误,但不知道为什么......是的,我已经检查了其他类似的帖子,但没有任何帮助。这是错误:

Uncaught ReferenceError: sendcard is not defined
onclick @ kikcards.html:102      

是:

<tr><td colspan=2 align=center><img src="/Images/generate.png" width=150 onclick="sendcard()"></td></tr>

我拥有的所有代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Kik Cards | Crazii</title>

    <link rel="stylesheet" type="text/css" href="/-default.css"/>
    <link href='https://fonts.googleapis.com/css?family=Permanent+Marker' rel='stylesheet' type='text/css'>
    <meta charset = "utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <link id="kikicon" rel="kik-icon" href="kik.png"/>
    <script src="/navOpen.js"></script>
    <script src="https://cdn.kik.com/kik/2.0.5/kik.js"></script>
</head>

<body class="body">

    <nav id="mainNav">
      <div id="logo">
        <a href="/index.html"><span>C</span>razii<span> </span> </a>
      </div>
      <div id="mobileToggle">
        <div class="bar"></div>
        <div class="bar"></div>
        <div class="bar"></div>
      </div>
      <ul id="mainMenu">
        <li>
          <a href="/index.html">Home</a>
        </li>
        <li>
          <a href="/about.html">About</a>
        </li>
        <li>
          <a href="#">PS3 Modding</a>
        </li>
        <li>
          <a href="/ebooks/ebooks.html">PDFs</a>
        </li>
        <li>
          <a href="/kikcards.html">Kik Cards</a>
        </li>
      </ul>
    </nav>

    <div class="mainContent">
        <div class="content">
            <article class="Content1">
                <header>
                    <h2><a href="#" title="1st Post"> Kik Cards</a></h2>
                </header>
                <footer>
                    <p class="post-info"> <br> </p>

                </footer>
                <content>
                    <p>
                        <table>
                            <tr><td style="text-align:right">Title:</td><td><input id="title"></td></tr>
                            <tr><td style="text-align:right">Text (optional):</td><td><input id="text"></td></tr>
                            <tr><td style="text-align:right">Main Title:</td><td><input id="maintitle"></td></tr>
                            <tr><td style="text-align:right">Little Icon (URL, optional):</td><td><input id="icon"></td></tr>
                            <tr><td style="text-align:right">Big Picture (URL, optional):</td><td><input id="pic">(If nothing is entered here, the default icon will be used)</td></tr>
                            <tr><td style="text-align:right">Redirect URL (optional):</td><td><input id="redirect"></td></tr>
                            <tr><td style="text-align:right">Big picture? (hides the title)</td><td><input type="checkbox" id="big"></td></tr> <!-- document.getElementById(big).checked -> bool -->
                            <tr><td style="text-align:right">Forward-able?</td><td><input type="checkbox" id="forward" checked=true></td></tr>
                            <tr><td colspan=2 align=center><img src="/Images/generate.png" width=150 onclick="sendcard()"></td></tr>
                        </table>
                    </p>
                </content>
            </article>
        </div>
    </div>
    <footer class="mainFooter">
        <p> Copyright &copy; 2015 <a href="#" title="Crazii"><b>Crazii</b></p>
    </footer>

    <script>
    window.onload = function()
        {
            if (kik.message && kik.message['url']!="") {
                    window.location.replace(kik.message['url']);
                }
                var title = "";
                var maintitle = "";
                var body = "";
                var display = "";
                var bigpicture = "";
                var forwardable = "";
                var redirect = "";
                function precede(str, check_for) {
                    return str.slice(0, check_for.length) == check_for;
                }
                function sendcard() {
                    title = document.getElementById("title").value;

                    maintitle = document.getElementById("maintitle").value;
                    if (maintitle=="") {
                        maintitle = "Kik Cards | Crazii";
                    }

                    body = document.getElementById("text").value;

                    document.getElementById("kikicon").href = document.getElementById("icon").value;

                    display = document.getElementById("pic").value;
                    if (display == "") {
                        display = "http://crazii.herobo.com/Images/kik.png";
                    }
                    bigpicture = document.getElementById("big").checked;
                    if (document.getElementById("forward").checked == true) {
                        forwardable = false;
                    } else {
                        forwardable = true;
                    }

                    redirect = document.getElementById("redirect").value;
                    if (precede(redirect, "http://")) {
                        redirect = redirect;
                    } else if (precede(redirect, "https://")) {
                        redirect = redirect;
                    } else if (redirect == "") {
                        redirect = "http://crazii.herobo.com";
                    } else {
                        redirect = "http://"+redirect;
                    }
                    document.getElementById("webtitle").innerHTML = maintitle;

                    kik.send({
                    title: title,
                    text: body,
                    pic: display,
                    big: bigpicture,
                    noForward: forwardable,
                    data: {'url': redirect}
                });
            }
        }
    </script>
</body>
</html>     

提前感谢我能得到任何帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

函数sendcard()是在window.onload回调范围内定义的,因为它在全局范围内不可见。

要自行启动,您可以使用addEventListener

您需要能够找到您的元素,例如通过向其添加id

<img src="/Images/generate.png" width=150 id="sendcard">

然后在window.onload中搜索该元素,并添加一个事件监听器:

document.getElementById("sendcard").addEventListener("click",sendcard, false);

你也可以考虑将sendcard定义移出回调,但是你会污染应该避免的全局范围。

答案 1 :(得分:0)

试试这个: -

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Kik Cards | Crazii</title>

    <link rel="stylesheet" type="text/css" href="/-default.css"/>
    <link href='https://fonts.googleapis.com/css?family=Permanent+Marker' rel='stylesheet' type='text/css'>
    <meta charset = "utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <link id="kikicon" rel="kik-icon" href="kik.png"/>
    <script src="/navOpen.js"></script>
    <script src="https://cdn.kik.com/kik/2.0.5/kik.js"></script>
</head>

<body class="body">

    <nav id="mainNav">
      <div id="logo">
        <a href="/index.html"><span>C</span>razii<span> </span> </a>
      </div>
      <div id="mobileToggle">
        <div class="bar"></div>
        <div class="bar"></div>
        <div class="bar"></div>
      </div>
      <ul id="mainMenu">
        <li>
          <a href="/index.html">Home</a>
        </li>
        <li>
          <a href="/about.html">About</a>
        </li>
        <li>
          <a href="#">PS3 Modding</a>
        </li>
        <li>
          <a href="/ebooks/ebooks.html">PDFs</a>
        </li>
        <li>
          <a href="/kikcards.html">Kik Cards</a>
        </li>
      </ul>
    </nav>

    <div class="mainContent">
        <div class="content">
            <article class="Content1">
                <header>
                    <h2><a href="#" title="1st Post"> Kik Cards</a></h2>
                </header>
                <footer>
                    <p class="post-info"> <br> </p>

                </footer>
                <content>
                    <p>
                        <table>
                            <tr><td style="text-align:right">Title:</td><td><input id="title"></td></tr>
                            <tr><td style="text-align:right">Text (optional):</td><td><input id="text"></td></tr>
                            <tr><td style="text-align:right">Main Title:</td><td><input id="maintitle"></td></tr>
                            <tr><td style="text-align:right">Little Icon (URL, optional):</td><td><input id="icon"></td></tr>
                            <tr><td style="text-align:right">Big Picture (URL, optional):</td><td><input id="pic">(If nothing is entered here, the default icon will be used)</td></tr>
                            <tr><td style="text-align:right">Redirect URL (optional):</td><td><input id="redirect"></td></tr>
                            <tr><td style="text-align:right">Big picture? (hides the title)</td><td><input type="checkbox" id="big"></td></tr> <!-- document.getElementById(big).checked -> bool -->
                            <tr><td style="text-align:right">Forward-able?</td><td><input type="checkbox" id="forward" checked=true></td></tr>
                            <tr><td colspan=2 align=center><img src="imgh.jpg" width=150 onclick="sendcard()" /></td></tr>
                        </table>
                    </p>
                </content>
            </article>
        </div>
    </div>
    <footer class="mainFooter">
        <p> Copyright &copy; 2015 <a href="#" title="Crazii"><b>Crazii</b></p>
    </footer>

    <script>

            if (kik.message && kik.message['url']!="") {
                    window.location.replace(kik.message['url']);
                }
                var title = "";
                var maintitle = "";
                var body = "";
                var display = "";
                var bigpicture = "";
                var forwardable = "";
                var redirect = "";
                function precede(str, check_for) {
                    return str.slice(0, check_for.length) == check_for;
                }
                function sendcard() {
                alert("sdf");
                    title = document.getElementById("title").value;

                    maintitle = document.getElementById("maintitle").value;
                    if (maintitle=="") {
                        maintitle = "Kik Cards | Crazii";
                    }

                    body = document.getElementById("text").value;

                    document.getElementById("kikicon").href = document.getElementById("icon").value;

                    display = document.getElementById("pic").value;
                    if (display == "") {
                        display = "http://crazii.herobo.com/Images/kik.png";
                    }
                    bigpicture = document.getElementById("big").checked;
                    if (document.getElementById("forward").checked == true) {
                        forwardable = false;
                    } else {
                        forwardable = true;
                    }

                    redirect = document.getElementById("redirect").value;
                    if (precede(redirect, "http://")) {
                        redirect = redirect;
                    } else if (precede(redirect, "https://")) {
                        redirect = redirect;
                    } else if (redirect == "") {
                        redirect = "http://crazii.herobo.com";
                    } else {
                        redirect = "http://"+redirect;
                    }
                    document.getElementById("webtitle").innerHTML = maintitle;

                    kik.send({
                    title: title,
                    text: body,
                    pic: display,
                    big: bigpicture,
                    noForward: forwardable,
                    data: {'url': redirect}
                });
            }

    </script>
</body>
</html> 

删除: - window.onload = function()         { 和结束图像标记