简体   繁体   中英

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. 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. 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.

what i need is to check the browser name and if its chrome then only need to display the top.php file. 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.

Try examining $_SERVER['HTTP_USER_AGENT'] .

See also: http://php.net/manual/en/function.get-browser.php

PHP is a sever side language. It runs inside server.

Javascript is client side language. In the sense, it runs in the browser

You cannot include aphp file from client side. Browser can only understand html, js css etc

You have to check the user agent in server array and load the page accordingly. you can use the

$_SERVER['HTTP_USER_AGENT']; or 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']; or even the built in getBrowser() function - 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 . The best way accroding to me is that

put this js script in the head than check the condition browser.

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>

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