简体   繁体   English

如何在javascript代码中包含php文件(top.php)?

[英]How to include a php file(top.php) inside a javascript code?

i want to include a php fie called top.php into my javascript code if it satisfy the condition. 如果要满足条件,我想在我的JavaScript代码中包含一个名为top.php的php fie。 This is a code for checking browser version. 这是用于检查浏览器版本的代码。 what i need is to check the browser name and if its chrome then only need to display the top.php file. 我需要的是检查浏览器名称,如果其镶边,则只需要显示top.php文件。 But in this code in every browser it include that page. 但是在每个浏览器的这段代码中,它都包含该页面。

<script type="text/javascript">
    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;
    var browserName  = navigator.appName;
    var fullVersion  = ''+parseFloat(navigator.appVersion); 
    var majorVersion = parseInt(navigator.appVersion,10);
    var nameOffset,verOffset,ix;

    // In Opera, the true version is after "Opera" or after "Version"
    if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
     browserName = "Opera";
     fullVersion = nAgt.substring(verOffset+6);
     if ((verOffset=nAgt.indexOf("Version"))!=-1) 
       fullVersion = nAgt.substring(verOffset+8);
    }
    // In MSIE, the true version is after "MSIE" in userAgent
    else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
     browserName = "Microsoft Internet Explorer";
     fullVersion = nAgt.substring(verOffset+5);
    }
    // In Chrome, the true version is after "Chrome" 
    else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
     browserName = "Chrome";
     fullVersion = nAgt.substring(verOffset+7);
    }
    // In Safari, the true version is after "Safari" or after "Version" 
    else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
     browserName = "Safari";
     fullVersion = nAgt.substring(verOffset+7);
     if ((verOffset=nAgt.indexOf("Version"))!=-1) 
       fullVersion = nAgt.substring(verOffset+8);
    }
    // In Firefox, the true version is after "Firefox" 
    else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
     browserName = "Firefox";
     fullVersion = nAgt.substring(verOffset+8);
    }
    // In most other browsers, "name/version" is at the end of userAgent 
    else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
              (verOffset=nAgt.lastIndexOf('/')) ) 
    {
     browserName = nAgt.substring(nameOffset,verOffset);
     fullVersion = nAgt.substring(verOffset+1);
     if (browserName.toLowerCase()==browserName.toUpperCase()) {
      browserName = navigator.appName;
     }
    }
    // trim the fullVersion string at semicolon/space if present
    if ((ix=fullVersion.indexOf(";"))!=-1)
       fullVersion=fullVersion.substring(0,ix);
    if ((ix=fullVersion.indexOf(" "))!=-1)
       fullVersion=fullVersion.substring(0,ix);

    majorVersion = parseInt(''+fullVersion,10);
    if (isNaN(majorVersion)) {
     fullVersion  = ''+parseFloat(navigator.appVersion); 
     majorVersion = parseInt(navigator.appVersion,10);
    }

    document.write(''
     +'Browser name  = '+browserName+'<br>'
     +'Full version  = '+fullVersion+'<br>'
     +'Major version = '+majorVersion+'<br>'
     +'navigator.appName = '+navigator.appName+'<br>'
     +'navigator.userAgent = '+navigator.userAgent+'<br>'
    )
    var OSName="Unknown OS";
    if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
    if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
    if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
    if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
    document.write('Your OS: '+OSName+'<br>');
    if(browserName ==  'Chrome')
    {
    document.write("u r using chrome "+fullVersion);
    <?php include("top.php") ?> ;

    }
    else {
    alert("Not chrome");
    }
    </script>

You can't and you shouldn't. 你不能,你不应该。 The browser doesn't execute any PHP code. 浏览器不执行任何PHP代码。

what i need is to check the browser name and if its chrome then only need to display the top.php file. 我需要的是检查浏览器名称,如果其镶边,则只需要显示top.php文件。 But in this code in every browser it include that page. 但是在每个浏览器的这段代码中,它都包含该页面。

I'd be against doing such things on the server side, but if you have to, you can try to check the browser through PHP by examining the request headers. 我反对在服务器端执行此类操作,但是如果有必要,可以尝试通过检查请求标头来通过PHP检查浏览器。

Try examining $_SERVER['HTTP_USER_AGENT'] . 尝试检查$_SERVER['HTTP_USER_AGENT']

See also: http://php.net/manual/en/function.get-browser.php 另请参阅: http : //php.net/manual/zh/function.get-browser.php

PHP is a sever side language. PHP是一种服务器端语言。 It runs inside server. 它在服务器内部运行。

Javascript is client side language. Javascript是客户端语言。 In the sense, it runs in the browser 从某种意义上说,它在浏览器中运行

You cannot include aphp file from client side. 您不能从客户端添加aphp文件。 Browser can only understand html, js css etc 浏览器只能理解html,js css等

You have to check the user agent in server array and load the page accordingly. 您必须检查服务器阵列中的用户代理并相应地加载页面。 you can use the 您可以使用

$_SERVER['HTTP_USER_AGENT']; $ _SERVER [ 'HTTP_USER_AGENT']; or getBrowser() 或getBrowser()

In order to decipher what browser your users are using and according to the results include separate PHP files, you can use the $_SERVER['HTTP_USER_AGENT']; 为了解释用户使用的浏览器,并根据结果包括单独的PHP文件,可以使用$_SERVER['HTTP_USER_AGENT']; or even the built in getBrowser() function - http://php.net/manual/en/function.get-browser.php 甚至内置的getBrowser()函数- http://php.net/manual/en/function.get-browser.php

<?php
  $browser = get_browser(null, true);
  print_r($browser);
?>

This code should give you an array similar to this - 此代码应为您提供类似于以下内容的数组-

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    [cssversion] => 2
    ...
)

You can then test to see what browser you user is using and include separate files accordingly. 然后,您可以进行测试以查看用户使用的浏览器,并相应地包含单独的文件。

Its not possible to include php code in js file . 不可能在js文件中包含php代码。 The best way accroding to me is that 对我来说最好的方法是

put this js script in the head than check the condition browser. 将这个js脚本放在首位,而不是检查条件浏览器。

if(browser =='chrome')
{
  document.getElementById('top').style.display='block';
}
else
{
  document.getElementById('top').style.display='none';
}

<div id='top'>
    <?php require_once("top.php"); ?>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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