0

I'm working on a legacy application built in jquery and PHP. I'm not sure how to assign the variable correctly. The PHP part is blank when a view source.

if(window.location.href.indexOd('age') >= 0){
  <?php
      $width = '750px'
  ?>
} else {
    $width = '500px'
}

The width variable is then assigned using jquery

$('#main-dev').attr('style', max-width:<?php echo $width;?>");

This isn't apply the correct width.

Any ideas why the php variable isn't taking?

The JS above appears in the source like

if(window.location.href.indexOd('age') >= 0){
} else {
}

I've never used PHP before, just trying to work on this legacy site?

  • `indexOd` should be `indexOf` – Swati Mar 15 '21 at 13:21
  • 1
    You're mixing up [client and server side programming](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). – El_Vanja Mar 15 '21 at 13:22
  • 1
    The whole question makes little sense. Of course your JS if/else blocks are empty - because you only assigned a value to a PHP variable in between those lines, but you did not create any output. And why involve PHP in the first place? You want to _set_ a value on the client side, based on another value you _read_ on the client side. – CBroe Mar 15 '21 at 13:25
  • You likely want to do something like this, `$('#main-dev').style('max-width', window.location.href.indexOf('age') >= 0 ? '750px' : '500px');` – CBroe Mar 15 '21 at 13:27

1 Answers1

0

Create a var "width" in JS global scope and change value. Attention, the correct is "indexOf".

var width = '500px';
if(window.location.href.indexOf('age') >= 0){
    width = '750px';
}

So, use the "width" var.

$('#main-dev').attr('style', 'max-width:' + width);