简体   繁体   中英

How to properly write this JS script in PHP echo?

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

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"

Here is an example of using javascript in php echo:

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

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

尝试这个,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM