Ritika mishra
Ritika mishra

Reputation: 19

How to use base url or php variable in javascript file

I am new in PHP, i want to use "base_url"(php variable) in javascript file,But right now i am unable to load image,Here is my current code,How can i do this ? Thanks in advance.

$('#site-logo').find('img').attr( {src:'<?php echo base_url(); ?>assets/images/logo/[email protected]',width:'151',height:'45'} );

Upvotes: 1

Views: 91

Answers (1)

JugalK
JugalK

Reputation: 146

You can create a variable or a constant in the javascript and store the base url there and use the same in the code.

Something along the line of following

var BASE_URL = "<?php echo base_url(); ?>";

$('#site-logo').find('img').attr({src: BASE_URL+'/assets/images/logo/[email protected]',width:'151',height:'45'});

Upvotes: 1

Related Questions