Vimal
Vimal

Reputation: 1146

Get baseurl in javascript file-codeigniter

I need to get the base url of the project in a javascript function. my code is

function add_more()
{
    var numrows=parseInt(document.frm_add_announcement.cnnumrows.value)+1;
    var strinner="";
    var ni = document.getElementById('content');
    var newdiv = document.createElement('div');
    var divIdName = 'my'+numrows+'Div1';
    newdiv.setAttribute('id',divIdName);
    document.frm_add_announcement.cnnumrows.value=numrows;
    //alert("asdAS");

      strinner="<div class='row'> <div class='col-md-3'><div class='form-group'><input type='text' id='imgcaption"+numrows+"' name='imgcaption"+numrows+"' placeholder='Attachment caption'  class='form-control'/></div> </div><div class='col-md-2'><div class='form-group'><input type='file' id='document"+numrows+"' name='document"+numrows+"' onclick='disableurl("+numrows+")' /></div></div><div class='col-md-1'><label for=''></label><br>&nbsp;&nbsp;<img src='../../../images/close3.png' title='Click to remove File'  id='clear_multiple"+numrows+"' height='25px'  style='cursor:pointer' onclick='remove("+numrows+")'/></div><div class='col-md-3'><div class='form-group'><input type='text'  id='url"+numrows+"' name='url"+numrows+"' placeholder='Attachment URL' onkeyup='disablefile("+numrows+")'   class='form-control'/></div></div><div class='col-md-3'><div class='form-group'><select class='form-control' id='target_type"+numrows+"' name='target_type"+numrows+"'  ><option>-Choose-</option><option>Same tab</option><option>New tab</option></select></div></div></div>";

 //$(".remove-btn").live('click',function() {
                //$(this).parent().remove();
            //});

    newdiv.innerHTML=strinner;
    ni.appendChild(newdiv);

}

need to get the baseurl in the src="" of the image in strinner.

Upvotes: 1

Views: 11985

Answers (4)

hrishi
hrishi

Reputation: 1656

If you are using external js file then You can create hidden field in view with value as

<input type="hidden" id="base" value="<?php echo base_url(); ?>">

Then access it in js using id like

var base_url = $('#base').val();

Upvotes: 7

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

In codeigniter no more headache on getting the base_url.Just Load url helper in controller as below.OR you can load it on application/config/autoload.php

$this->load->helper('url');

Then inside script of your php file...

<script>
var base_url = <?php echo base_url(); ?>
alert(base_url);
</script>

Set base_url inside application/config/config.php

$config['base_url'] = 'http://domain_name/';

Upvotes: 2

Marprin
Marprin

Reputation: 196

You can initiate a JavaScript variable in your PHP file and then access the variable in any JavaScript file.

<script>
    var base_url = "<?= base_url('') ?>";
</script>

You have to load other JavaScript files and script tags after the above script to ensure the base_url variable is defined before it is referenced.

Upvotes: 3

Shahzad Riaz
Shahzad Riaz

Reputation: 354

You can access the current url quite easily in JavaScript with window.location

You have access to the segments of that URL via this locations object. For

Example:

var base_url = window.location.origin;
// "http://stackoverflow.com"

var host = window.location.host;
// stackoverflow.com

var pathArray = window.location.pathname.split( '/' );

Upvotes: 1

Related Questions