当页面为index.php时运行html代码

Running html code when page is index.php

本文关键字:运行 html 代码 php index      更新时间:2023-09-26

我需要运行以下代码:

<font size='3'><b>
    <li class="icon-home">
        <a href="{U_INDEX}" accesskey="h">{L_INDEX}</a> <!-- BEGIN navlinks -->
        <strong>&#8249;</strong>
        <a href="{navlinks.U_VIEW_FORUM}">{navlinks.FORUM_NAME}</a><!-- END navlinks -->
    </li>
</b></font>

当网站位于index.php.时

我试过这个:

<script type="text/javascript">
    $(document).ready(function () {
        if(window.location.href.indexOf("index.php") > -1) {
           document.write('//the above HTML');
        }
    });
</script>

我不能使用php,因为我使用的是html文件。上面的代码只给了我董事会的链接,其他什么都没有。我需要显示链接以及页面的其余部分。

谢谢你的帮助。

您应该使用div或其他元素作为占位符,而不是document.write

if (location.href.indexOf("/index.php") > -1 ) {
   document.getElementById("myPlaceHolder").innerHTML = '<font size="3"><b><li class="icon-home"><a href="{U_INDEX}" accesskey="h">{L_INDEX}</a><strong>&#8249;</strong> <a href="{navlinks.U_VIEW_FORUM}">{navlinks.FORUM_NAME}</a></li></b></font>';
}

在HTML中,只需添加一个div即可显示内容。

<div id="myPlaceHolder"></div>

请确保您的scriptdiv标记之后运行,否则document.getElementById("myPlaceHolder")将返回null

您的代码包含Smarty代码,这是一种PHP。你可以使用

{if $smarty.server.REQUEST_URI == '/index.php'} 
    //Your HTML stuff here
{/if}

编辑:我应该适当地提到,{东西}是聪明的代码。它就像html文件中的php代码。它由php进行解释。这意味着,用javascript插入它是不可能的,因为PHP(因此,Smarty)在服务器上被解释,而Javscript在客户端执行,也就是说,你在家里的PC上。