0

I'd like to print this JS script.

<script src="<?php echo base_url('assets/inspinia/js/jquery-2.1.1.js'); ?>"></script>
<script src="<?php echo base_url('assets/jquery-ui/jquery-ui.js'); ?>"></script>

I write like this:

print("<script type=\'text/javascript\' src=\'".base_url('assets/inspinia/js/jquery-2.1.1.js')."\'></script>");
print("<script type=\'text/javascript\' src=\'".base_url('assets/jquery-ui/jquery-ui.js')."\'></script>");

and this:

echo "<script type=\'text/javascript\' src=\'".base_url('assets/inspinia/js/jquery-2.1.1.js')."\'></script>";
echo "<script type=\'text/javascript\' src=\'".base_url('assets/jquery-ui/jquery-ui.js')."\"></script>";

But, both method can't get the external JS file.

Note: The html in in a modal

7
  • try this:- echo "<script type='text/javascript' src=' ".base_url('assets/inspinia/js/jquery-2.1.1.js')." '></script>"; Commented Jul 11, 2017 at 5:21
  • It's not called the JS. if I try to comment // the echo. It turn yellow until befor the </script>. Commented Jul 11, 2017 at 5:24
  • did you check the source when added the code like in comment? getting any error? Commented Jul 11, 2017 at 5:26
  • yes. using usual javascript it's called the js file. but using PHP echo, it's not. Commented Jul 11, 2017 at 5:27
  • not with usual script.. i am saying about code in comment. did you get any error when you try this code. check first comment Commented Jul 11, 2017 at 5:28

4 Answers 4

1

To simplify, I will first give you an example with the url saved as a variable.

$url = base_url('assets/inspinia/js/jquery-2.1.1.js');

echo "<script src='$url'></script>";

You can keep it in the long form if you prefer, but you would need to leave the quotes and concatenate (similar to what you have been doing)

echo "<script src='" . base_url('assets/inspinia/js/jquery-2.1.1.js') . "'></script>";

For that reason, I always prefer to assign it to a variable first so that I can see the separation of "get value" and then "print string including that value"

Sign up to request clarification or add additional context in comments.

1 Comment

It's calling the JS file. But it messed up my modal.
0

Here is an example of using javascript in php echo:

<?php
echo '<script>Hello World!</script>';
?>

2 Comments

I need an example using the base_url()
check comment under OP
0

I'm a bit sorry to see what you mean, is that so?

echo "<script  type='text/javascript'>
function getState(){            
        var cs = ".$state.";
        return cs;
} 
</script>";

Do you want to introduce JS files in PHP? You can wrap yourself,and you can refer to YII2's wording, like this:

/**
 * 定义按需加载JS方法
 * @param $view View
 * @param $jsfile
 */
public static

function addScript($view, $jsfile) {
  $AssetManager = new AssetManager();
  $jsfile = $AssetManager - > getPublishedUrl('@backend/modules/motorcade/assets').$jsfile;
  $view - > registerJsFile($jsfile, [motorcadeUIAsset::className(), 'depends' => 'backend\modules\motorcade\assets\motorcadeUIAsset']);
}

Hope it helps you

Comments

0

Try this,

echo "<script src=" '. base_url('assets/inspinia/js/jquery-2.1.1.js') .' "></script>";

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.