2015-09-04 37 views
3

我安裝我下我的Apache /本地主機/ ci_app子目錄CI/JS動態鏈接指向根/在CI

現在我下/本地主機/ ci_app一些JS文件/資產/ JS/code.js

裏面code.js我加載腳本動態

(function(d, s, id){ 
    var js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) {return;} 
    js = d.createElement(s); js.id = id; 
    js.src = "/assets/js/another_script.js"; 
    fjs.parentNode.insertBefore(js, fjs); 
}(document, 'script', 'my_other_script')); 

在CI配置我有$config['base_url'] = '/ci_app/';但每次我加載頁面我得到的腳本中調用http://localhost/assets/js/another_script.js

我需要的腳本是指向http://localhost/ci_app/assets/js/another_script.js

+0

嘗試讓base_url在配置中爲空,CI在猜測它時非常神。然後,嘗試像這樣調用腳本:js.src =「<?php echo base_url();?> assets/js/another_script.js」;它應該工作... – sinisake

+0

這不會工作。不執行裏面.js – Kal

+0

對不起,沒有仔細閱讀,認爲這是你的看法... – sinisake

回答

0

您可以添加<base href="<?php echo base_url(); ?>">頁面的<head>內。這解決了所有相對路徑到您的$config['base_url']設置。

1

你能做到這一點有兩種方式,

你的js腳本可以調用這個函數,它會給你的基本URL你想

(function(d, s, id){ 
    var js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) {return;} 
    js = d.createElement(s); js.id = id; 
    js.src = getBaseUrl() + "/assets/js/another_script.js"; 
    fjs.parentNode.insertBefore(js, fjs); 
}(document, 'script', 'my_other_script')); 

function getBaseUrl() { 
    var l = window.location; 
    var base_url = l.protocol + "//" + l.host + "/" + l.pathname.split('/')[1]; 
    return base_url; 
} 

,你可以改變你的BASE_URL在配置這樣的

$config['base_url'] = 'http://localhost/ci_app/'; 

它可以工作。