Answer the question
In order to leave comments, you need to log in
Why doesn't php to javascript work in Yii2?
I register javascript code in main.php
But it does not see the function <?php Yii::getAlias("@web")?>
How can I add an alias to the image path?
<?php $this->registerJs(
'jQuery(document).ready(function($){
var smallWindow = false;
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 50) {
$("#logo-image").attr("src", "<?php Yii::getAlias("@web")?>/images/logos/Monogram.png")
$(".important-class").addClass("padding-on-my-header");
}
if (scroll < 50) {
$(".important-class").removeClass("padding-on-my-header");
$("#logo-image").attr("src", "<?php Yii::getAlias("@web")?>/images/logos/PetCare_Monogram.png")
}
}).resize(function(){
if ( !smallWindow && this.innerWidth <= 1024 ) {
smallWindow = true;
$(".top-bar-section").find("ul.right").hide(0).delay(500).show(0);
}
if ( smallWindow && this.innerWidth > 1024 ) {
smallWindow = false;
}
});
});');
?>
Answer the question
In order to leave comments, you need to log in
<?php $this->registerJs(
'jQuery(document).ready(function($){
var smallWindow = false;
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 50) {
$("#logo-image").attr("src", "'.Yii::getAlias("@web").'/images/logos/Monogram.png")
$(".important-class").addClass("padding-on-my-header");
}
if (scroll < 50) {
$(".important-class").removeClass("padding-on-my-header");
$("#logo-image").attr("src", "'.Yii::getAlias("@web").'/images/logos/PetCare_Monogram.png")
}
}).resize(function(){
if ( !smallWindow && this.innerWidth <= 1024 ) {
smallWindow = true;
$(".top-bar-section").find("ul.right").hide(0).delay(500).show(0);
}
if ( smallWindow && this.innerWidth > 1024 ) {
smallWindow = false;
}
});
});');
?>
I am not a Yii expert, but I can assume that due to the fact that you have a php tag in the php tag
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question