CodeIgniter Forums
[Solved] Setting url in a .js file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [Solved] Setting url in a .js file (/showthread.php?tid=66538)



[Solved] Setting url in a .js file - wolfgang1983 - 11-01-2016

I have set my url in a js file


Code:
assets

assets > jquery > common.js



And I have a controller called Filemanager.php


Code:
application

application > controllers > Filemanager.php


When I try click on my button which trys to launch modal. It gives the wrong location and a 404 error


The url out puts

Code:
http://localhost/project-1/question/filemanager

How ever should only produce

Code:
http://localhost/project-1/filemanager


assets > jquery > common.js


Code:
$(document).ready(function() {
    $(document).delegate('img[data-toggle=\'image\']', 'click', function() {
        $('#modal-image').remove();
        $.ajax({
            url: 'filemanager',
            dataType: 'html',
            success: function(html) {
                $('body').append('<div id="modal-image" class="modal">' + html + '</div>');
    
                $('#modal-image').modal('show');
            }
        });
    });    
});   


Routes.php


Code:
$route['filemanager'] = 'filemanager/index';


Any idea in how to make sure it gets the right url I cannot use base_url in a js file.


RE: Setting url in a .js file - salain - 11-01-2016

As a work around I have a view that is use in every pages.
The view is to set default styles and Javascript for the site. This include  a JavaScript variable for the site URL

PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url().index_page();?>";
</
script

Then use the variable to make the URL
Code:
$(document).ready(function() {
    $(document).delegate('img[data-toggle=\'image\']', 'click', function() {
        $('#modal-image').remove();
        $.ajax({
            url: BASE_URL + '/filemanger', // full URL
            dataType: 'html',
            success: function(html) {
                $('body').append('<div id="modal-image" class="modal">' + html + '</div>');
    
                $('#modal-image').modal('show');
            }
        });
    });    
});  

I hope this helps.


RE: Setting url in a .js file - wolfgang1983 - 11-02-2016

(11-01-2016, 10:45 PM)salain Wrote: As a work around I have a view that is use in every pages.
The view is to set default styles and Javascript for the site. This include  a JavaScript variable for the site URL

PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url().index_page();?>";
</
script

Then use the variable to make the URL
Code:
$(document).ready(function() {
    $(document).delegate('img[data-toggle=\'image\']', 'click', function() {
        $('#modal-image').remove();
        $.ajax({
            url: BASE_URL + '/filemanger', // full URL
            dataType: 'html',
            success: function(html) {
                $('body').append('<div id="modal-image" class="modal">' + html + '</div>');
    
                $('#modal-image').modal('show');
            }
        });
    });    
});  

I hope this helps.

I can access the url in browser but when use ajax says network error even though url is correct now.


RE: Setting url in a .js file - salain - 11-02-2016

I never had any issues but I have no route setup for url I use with ajax.
Maybe ajax by pass the route, but that being said it should work as the index method is default.

Sorry, I don't know what could be the problem.


RE: Setting url in a .js file - InsiteFX - 11-02-2016

PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url();?>";
</
script



RE: Setting url in a .js file - wolfgang1983 - 11-02-2016

(11-02-2016, 03:07 AM)InsiteFX Wrote:
PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url();?>";
</
script

Your one worked plus on common.js miss spelt also filemanager so both now are fixed.


RE: Setting url in a .js file - InsiteFX - 11-02-2016

(11-02-2016, 03:20 AM)wolfgang1983 Wrote:
(11-02-2016, 03:07 AM)InsiteFX Wrote:
PHP Code:
<script type="text/javascript">
 
   var BASE_URL "<?php echo base_url();?>";
</
script

Your one worked plus on common.js miss spelt also filemanager so both now are fixed.


You can also do the same using site_url if you need the index.php