2016-01-22 124 views
0

我在url上设置了一个条件语句,我测试了IF和ELSE,它可以用一个简单的document.write。不过,我想输出的PHP我的条件,但它只是始终输出else语句,PHP是不是我的最强的语言:JavaScript有条件的忽略IF

<script type="text/javascript"> 

if(
window.location.href=="mywebsite.com" 
) 
{ 

    document.write('<label for="qty"><?php echo $this->__("Qty:") ?></label><input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />') 

}else{ 

    document.write('<label for="qty"><?php echo $this->__(' ') ?></label> 
     <input type="hidden" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />') 

} 

</script> 

回答

1

window.location.href包含完整的URL。如果您只想检查URL的主机名部分,请使用window.location.host

if (window.location.host == "mywebsite.com") { 
    ... 
} 

另一种选择是在服务器上完成所有工作。

<?php 
if ($_SERVER['SERVER_NAME'] == 'mywebsite.com') { 
?> 
<label for="qty"><?php echo $this->__("Qty:") ?></label> 
    <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" /> 
<?php } else { 
?> 
<label for="qty"><?php echo $this->__(' ') ?></label> 
    <input type="hidden" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" /> 
<?php } 
+0

问题是,我在IF语句中发出了一个警告,并且它的工作原理,但是因为我在If和Else中包含'doucment.write',它只显示Else关于我是哪个页面因为我想根据特定的URL更改内容 – PhpDude

+0

为什么你在JS中这样做呢?在PHP中执行:'if($ _SERVER ['SERVER_NAME'] =='mywebsite.com'){echo ...} else {echo ...}' – Barmar

0

window.location.href返回完整的URL包括协议(所以http://www.mywebsite.com)。

您可以提醒window.location.href的值以准确查看它返回的内容,并更新if语句以匹配它。

+0

的事情是,我把警报IM IF语句和它的工作,但因为我包含在如果'doucment.write'和否则它只显示否则关于我的页面上,我想根据特定的URL更改内容 – PhpDude