简体   繁体   English

从ajax加载的脚本访问JavaScript函数

[英]Access JavaScript functions from scripts loaded from ajax

I have a project in PHP using CodeIgniter in an HMVC approach. 我在HMVC方法中使用CodeIgniter的PHP项目。

Now, I run into a problem where I'm really confused what to do next. 现在,我遇到了一个问题,我真的很困惑下一步做什么。 Here's my situation: 这是我的情况:

I have a view which looks like this: 我有一个看起来像这样的视图:

//logs_v_month.php

<div id="logs" class="tab-pane active">

</div>

<script type="text/javascript">
$(function(){       
    $.ajax({
      url: "<?php echo load_global_assets('system/js/log.js')?>",
      dataType: "script",
      success: function(e){}
    });
});

</script>

Then I have a controller: 然后我有一个控制器:

//view.php
function display_logs_month(){
    $data['logs_v'] = $this->load->view('logs_v_month');                        

echo json_encode($data);
}

Then I have another view which is the main view. 然后我有另一个视图,这是主视图。

//logs.php
<div class="tab-content" id="logs_content">
    <!--logs loaded via ajax call-->
</div>
<script type="text/javascript">
function view_month(group_id, start_end){
$.ajax({
    type : "POST",
    dataType: "json",
    url : "<?=base_url()?>logs/view/display_logs_month",                        
    data : {"group_id" : group_id},
    success : function (data){  
        $("div#logs").remove();                     
        $("#logs_content").append(data.logs_v);//logs_v             
    },
    error : function(data){
    console.log(data);
    }
    });
}

The logs.php view has an ajax call to load the logs_v_month.php view through the view.php controller. logs.php视图有一个ajax调用,用于通过view.php控制器加载logs_v_month.php视图。

My question is, how do I access JavaScript functions (which are also loaded via ajax) from the logs_v_month.php from the logs.php view? 我的问题是,如何从logs.php视图中的logs_v_month.php访问JavaScript函数(也通过ajax加载)?

I've tried searching for the answer on the web and most answers people give is using the eval() function but many discourages from using that. 我尝试在网上搜索答案,人们给出的大多数答案都是使用eval()函数,但许多人不鼓励使用它。

Is there a way I can restructure or rearrange my JavaScript so it will be easy for me to access them in this kind of situation? 有没有办法可以重构或重新安排我的JavaScript,这样我就可以轻松地在这种情况下访问它们? I'm also taking into account Namespacing if this can help the problem. 我也考虑了Namespacing,如果这可以帮助解决问题。

I just started learning CI and HMVC so I'm fairly new to this. 我刚开始学习CI和HMVC,所以我对此很新。

Thanks! 谢谢!

You need to put the returned scripts into the DOM for the page. 您需要将返回的脚本放入页面的DOM中。

when you get the script data back in the success method of your ajax call, you can create a script element with the code inside it and append it to the DOM, something like: 当你在ajax调用的success方法中获得脚本数据时,可以使用其中的代码创建一个脚本元素并将其附加到DOM,如:

success:function(data){
    var scriptElement = document.createElement('script');
    scriptElement.setAttribute('type', 'text/javascript');
    scriptElement.textContent = data;
    var head = document.getElementsByTagName('head').item(0);
    head.appendChild(scriptElement);
}

Now the functions will be accessible to all scripts. 现在所有脚本都可以访问这些函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM