【问题标题】:Terminating the code if javascript is not found如果找不到 javascript,则终止代码
【发布时间】:2013-04-18 07:13:02
【问题描述】:

如果浏览器中没有启用 javascript,我会尝试显示一条消息,然后我不想进一步加载页面。我用php所以我做了

<?php exit;?>

在 noscipt 中,但尽管浏览器启用了 javascript,但 php 代码退出工作...... 我使用的代码是这样的:

<noscript>
        <div style="text-align:center; margin-top:15px; color:#f00; margin-bottom:10px;text-decoration: blink;">
            <h1>
                Please enable javascript<br style="margin-bottom: 5px;"/>
                <h3>Else the system will not work correctly.</h3>
            </h1>
        </div>
        <div align="center">
            <div class="title" style="font:bold 18px arial">Enabling Javascript</div>
            For: <strong>FireFox</strong>
            Go To:<strong>Options > Content</strong><br/> and Check the Enable Javascript.
            <br/>OR<br/>
            Refer this link to enable javascript according to your browser<br/>
            <a href="http://www.enable-javascript.com/">http://www.enable-javascript.com/</a>
            <?php exit;?>
        </div>
    </noscript>

如果未启用 javascript,我应该如何不继续进行,如果启用了 js,我应该继续...

【问题讨论】:

  • PHP 在服务器端运行,而&lt;noscript&gt; 是客户端元素。 PHP 生成内容(无论是什么内容)并将它们发送到客户端然后 浏览器呈现它们。 PHP 不知道内容是/应该如何呈现的。

标签: javascript noscript


【解决方案1】:

noscript 块在禁用 javascript 时执行,用于显示替代内容。

<noscript>
    <b>You don't have javascript enabled.</b>
</noscript>

如果禁用了 javascript,您可以将用户重定向到另一个页面。

<!DOCTYPE html>
<html lang="en">
    <head>
        <noscript>
            <meta http-equiv="refresh" content="0; /?javascript=no">
        </noscript>
        <meta charset="UTF-8"/>
        <title></title>
    </head>
</html>

在你的php代码中你可以检查

if(isset($_GET['javascript']) && $_GET['javascript'] == 'no')
{
    exit;
}

【讨论】:

  • 我的问题的解决方案是 if 语句,但效果不佳...没有任何响应
【解决方案2】:

您可以使用重定向,因此您可以将用户发送到一个页面,其中包含“抱歉,如果禁用 JAVASCRIPT,您将无法访问该站点”之类的消息。

 <noscript>
 <meta http-equiv="refresh" content="0; url=http://www.yourdomain.com/nojsmessage.php" />
  </noscript>

【讨论】:

    【解决方案3】:

    使用&lt;noscript&gt;标签-

    <noscript>Your browser does not support JavaScript!</noscript>
    

    你也可以试试这个

    if(isset($_GET['nojs']) && !isset($_SESSION['no_script']))
    {
        $_SESSION['no_script'] = true; //Or cookie
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 2018-07-06
      相关资源
      最近更新 更多