1

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'} );

5
  • Have you checked the value base_url returns? It should contain the trailing slash too. An easy way probably would be just a relative URL: '/assets/images/logo/[email protected]', no need for PHP. Commented Nov 18, 2022 at 6:33
  • @Teemu: actually i need to give "dynamic url" (base_url) which mentioned in "config.php" so thats why i want to use php variable,hope you understand my point Commented Nov 18, 2022 at 6:46
  • 1
    So what does the JavaScript code this generates actually look like? Did this piece of code even get sent through the PHP parser in the first place? Commented Nov 18, 2022 at 7:13
  • try to use "" for <?php tag and also concat that string to it also check if the final url generating is targetting the asset or not Commented Nov 18, 2022 at 7:29
  • @RohannnSingh can you please update my answer/write exact code so i can check and i can implement at my side Commented Nov 18, 2022 at 7:34

1 Answer 1

1

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

2 Comments

@Jigalk sorry but its not working
@Ritikamishra please try to check where are the assets file and then try to inspect and see what asset url is created at src Then compare both url are they same or not

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.