如何在CodeIgniter的特定页面中运行自定义JavaScript代码?

发布于 2025-02-02 02:52:34 字数 2865 浏览 6 评论 0原文

我有要在CodeIgniter页面的特定页面上打开的模型框(它如何工作)。

在我看来,以下代码适用于模型框:

<!-- Model -->
<div class="modal" id="how_its_modal" role="dialog" style="display: none;">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                
                <h4 class="modal-title">How Its Work Video</h4>
            </div>
            <div class="modal-body">
                <iframe width="100%" height="315" src="https://www.youtube.com/embed/DIx_xxxxx">
                </iframe>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<?php
    $custom_js = "$('#how_its_modal').modal('show');";
    //$custom_js = "This is okay";
?>

在我的控制器中,以下代码:

public function privacy_policy()
    {
        $this->load->view('header');
        $this->load->view('privacy-policy');
        $this->load->view('footer');
    }
    public function help_center()
    {
        $this->load->view('header');
        $this->load->view('help-center');
        $this->load->view('footer');
    }
    public function how_it_works()
    {
        $this->load->view('header');
        $this->load->view('how-it-works');
        $this->load->view('footer', ['custom_js' => $custom_js]);
    }
    public function mart()
    {
        $this->load->view('header');
        $this->load->view('mart');
        $this->load->view('footer');
    }

因此,我有多个页面。因此,现在我只想在页面上的如何工作中仅在how_it_works函数中运行以下代码。

<script>
    $('#how_its_modal').modal('show')
</script>

我的页脚代码

<script src="<?php echo SITE_PATH; ?>/front/js/jquery.min.js"></script>
<script src="<?php echo SITE_PATH; ?>/front/js/bootstrap.min.js"></script>
<script src="<?php echo SITE_PATH; ?>/front/js/bootstrap.bundle.min.js"></script>
<script src="<?php echo SITE_PATH; ?>/front/js/owl.carousel.min.js"></script>
<script src="<?php echo SITE_PATH; ?>/front/js/aos.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/10.16.0/sweetalert2.min.js" type="text/javascript"></script>
<?php echo $custom_js; ?>
<?php if(isset($custom_js)) { ?>
    <script><?php echo $custom_js; ?></script>
<?php }else{
    echo "Nothing Run";
} ?>

无法将此脚本放在页脚中,因为如果我愿意放置,它将在我不想要的所有页面上运行。

I have Model Box which I want to open on a specific page of the codeigniter page (How it work).

In My View, the following code is for the model box:

<!-- Model -->
<div class="modal" id="how_its_modal" role="dialog" style="display: none;">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                
                <h4 class="modal-title">How Its Work Video</h4>
            </div>
            <div class="modal-body">
                <iframe width="100%" height="315" src="https://www.youtube.com/embed/DIx_xxxxx">
                </iframe>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<?php
    $custom_js = "$('#how_its_modal').modal('show');";
    //$custom_js = "This is okay";
?>

In my Controller, the following code:

public function privacy_policy()
    {
        $this->load->view('header');
        $this->load->view('privacy-policy');
        $this->load->view('footer');
    }
    public function help_center()
    {
        $this->load->view('header');
        $this->load->view('help-center');
        $this->load->view('footer');
    }
    public function how_it_works()
    {
        $this->load->view('header');
        $this->load->view('how-it-works');
        $this->load->view('footer', ['custom_js' => $custom_js]);
    }
    public function mart()
    {
        $this->load->view('header');
        $this->load->view('mart');
        $this->load->view('footer');
    }

So here I have multiple pages. So now I want to run the following code in only the how_it_works function means on the page How it Works.

<script>
    $('#how_its_modal').modal('show')
</script>

My Footer Code

<script src="<?php echo SITE_PATH; ?>/front/js/jquery.min.js"></script>
<script src="<?php echo SITE_PATH; ?>/front/js/bootstrap.min.js"></script>
<script src="<?php echo SITE_PATH; ?>/front/js/bootstrap.bundle.min.js"></script>
<script src="<?php echo SITE_PATH; ?>/front/js/owl.carousel.min.js"></script>
<script src="<?php echo SITE_PATH; ?>/front/js/aos.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/10.16.0/sweetalert2.min.js" type="text/javascript"></script>
<?php echo $custom_js; ?>
<?php if(isset($custom_js)) { ?>
    <script><?php echo $custom_js; ?></script>
<?php }else{
    echo "Nothing Run";
} ?>

I can not put this script in the footer because if I will put then it will run on all pages which I do not want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2025-02-09 02:52:34

如果($ this-&gt; router--&gt; fetch_class (

)=='您的类名称'){'您的脚本}

You do with a particular class like you can check your current class and match it like this

if ($this->router->fetch_class() == 'YOUR CLASS NAME'){'Your script}

寄居人 2025-02-09 02:52:34

为什么您不尝试将脚本模式放入页面如何?然后将您的jQuery移至标头。
容易,但是最好您对模态有一个视图。

$this->load->view('header');
$this->load->view('help-center');
$this->load->view('footer');
$this->load->view('modal');

Why you not try to put your script modal inside your page how-it-works? then move your jquery to header.
easy, but it's better you make one view for modal so you have.

$this->load->view('header');
$this->load->view('help-center');
$this->load->view('footer');
$this->load->view('modal');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文