1

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.

2
  • 1
    use <?php echo baseurl();?>if it is already defined in config file. Commented Jan 10, 2017 at 6:19
  • 2
    If your js code is in php file you can use var path=<?php echo base_url('') ?>. If its a js file then you need to add the above line in php while were the js file included. Now the js file can access the js variable. Commented Jan 10, 2017 at 6:29

4 Answers 4

7

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Although this is working, this should not be marked as an answer. It's a very ugly workaround.
3

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.

3 Comments

but when iam entering the baseurl in src="" the url gets concatenated with the current url when page is loaded
have you setting your config.php file for 'base_url' to your default URL?@VimalS
do you put the last slash in your config.php file? can you show the 'base_url' in your config file? @VimalS
2

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/';

Comments

1

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( '/' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.