dtwr2012 2014-05-08 13:14
浏览 53
已采纳

如何在页面加载时使用JavaScript Alertify?

I have a form in a page that submits to itself. In the PHP portion I check

if($_SERVER['REQUEST_METHOD'] == 'POST')

and then I run few things. I am attempting to show an alert using the Alertify suit if something was posted. But on page load I get error

alertify is not defined

I guess it only does that because the page did not load the include links to the js/css files yet (looking in FireFox's FireBug I see the Alertify script line loading before the <html> tag). What can I do to make it work right?

UPDATE CODE:

<?php 
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        echo '<script type="text/javascript">alertify.alert("Hello");</script>';
    }
?>
<html>
    <head>
        <title>Hello There</title>
        <!--  Alertify Includes -->
        <script type="text/javascript" src="alertify.js"></script>
        <link rel="stylesheet" type="text/css" href="alertify.core.css">
        <link rel="stylesheet" type="text/css" href="alertify.default.css">        
    </head>
    <body onLoad="onLoad()">
        <form action="" method="POST" id="CONFIRM">                          
            <input  class="txt" value="" type="text" name="myName" id="myID" onKeyUp="" onChange="" onFocus="" />
        </form>
    </body>
</html>
  • 写回答

3条回答 默认 最新

  • dswy34539 2014-05-14 09:55
    关注

    After you posted the sources, I think I know what is wrong:

    <?php 
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
        echo '<script type="text/javascript">alertify.alert("Hello");</script>';
        }
    ?>
    

    is located above the script tag that loads your javascript. This means that any code in the PHP segment is executed before that code. In other words, alertify has not yet been defined there.

    To resolve this, place a PHP echo inside the head, below the include for alertify, or include the alertify as a separate echo in the PHP, example:

    <?php 
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
        echo '<script type="text/javascript" src="alertify.js"></script>'
        echo '<script type="text/javascript">alertify.alert("Hello");</script>';
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了