#1
  1. No Profile Picture
    Demonic Swordsman DGQB
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2003
    Posts
    1,043
    Rep Power
    83

    Simple JavaScript error in PHP


    In my main.php file, I wrote a javascript that detects the user browser and loads the appropriate page for SSI. The page is HERE, and the code follows. The problem seems to be that no browser can interpret this javascript, and the end of the script shows up on the page as "; } //-->" However, it does load the first page by default. Any help would be kindly appreciated.

    <script language="JavaScript">
    <!--

    //Loads JavaScript if using Netscape; loads VBScript if using IE

    browser_name = navigator.appName;

    if (browser_name == "Netscape")
    {
    <?php
    virtual ("jdate.html"); //jdate.html contains jscript
    ?>;
    }
    else if (browser_name == "Microsoft Internet Explorer")
    {
    <?php
    virtual ("menu.html");//menu.html is vbscript
    ?>;
    }


    //-->
    </script>
    Last edited by vb.net; April 13th, 2003 at 03:03 AM.
  2. #2
  3. No Profile Picture
    Demonic Swordsman DGQB
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2003
    Posts
    1,043
    Rep Power
    83
    Help
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    195
    Rep Power
    12

    Try this


    PHP Code:

    <?php

    echo "<script language='Javascript' type='text/Javascript'>

    <!--
    if ((navigator.appName=='Microsoft Internet Explorer') || (navigator.appName=='Netscape'))
    {     
      if (navigator.appName=='Microsoft Internet Explorer')
      window.location = 'browser_ie.htm'; //IE location
      else
      window.location = 'browser_ns.htm'; //NS location
      }
    else
    window.location = 'browser_other.htm'; // Other browser location
    //-->
    </script>"
    ;

    ?>
    Just change locations to your own needs.
  6. #4
  7. CSKA Sofia
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Location
    Germany / Bulgaria
    Posts
    575
    Rep Power
    123
    rustlers solution is good

    it's all becouse ssi goes also trough parser and you know JS is client side and is executed after its complete DW ... blah blah ...

    your algoritm was wrong
    Nikola Ivanov
    http://enikola.de
  8. #5
  9. No Profile Picture
    Demonic Swordsman DGQB
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2003
    Posts
    1,043
    Rep Power
    83
    rustler,

    Thanks for your help, but your solution is not what I had in mind. The goal is to detect the browser and include either browser_ie.html or browser_ns.html in the current file. Your script simply replaces the current page with browser_ie.html, whereas I want to include it to the left as a menu. Can you figure something out from that?

    So, I believe the script should look something like this (but it doesn't work yet result @ http://web.ics.purdue.edu/~lin5/proj2/2/main.php):

    PHP Code:
    <?php

    echo "<script language='Javascript' type='text/Javascript'>

    <!--
    if ((navigator.appName=='Microsoft Internet Explorer') || (navigator.appName=='Netscape'))
    {     
      if (navigator.appName=='Microsoft Internet Explorer')"
    ;

                          
    virtual ("menu.html");
    echo 
    "else";
        
    virtual ("jdate.html");

    echo 
    "</script>";

    ?>
    Last edited by vb.net; April 13th, 2003 at 06:19 PM.
  10. #6
  11. Super Genius
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Orlando, Florida
    Posts
    314
    Rep Power
    13
    vb.net:

    Unfortunately you are trying to do the impossible. You can't mix client-side and server-side technologies the way you are attempting to do so. I would suggest something along the following lines:[list=1][*]Use JavaScript to Detect the browser type[*]Have JavaScript determine which include file to pull into the page based upon the above result.[*]Use a client side <iframe> tag with the appropriate source file.[/list=1]
    Sleep is highly overrated.
  12. #7
  13. No Profile Picture
    Demonic Swordsman DGQB
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2003
    Posts
    1,043
    Rep Power
    83
    Actually, the easiest thing would have been to use a .shtml file for SSI, so as to avoid any server-side technology, but unfortunately SSI only works on my my server via server side files like ASP and PHP. At this point I think I will just include one file by default.
  14. #8
  15. Super Genius
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Orlando, Florida
    Posts
    314
    Rep Power
    13

    Try this:


    Give this code a try. Keep in mind my JavaScript skills are not very good so it might not work. This should give you an idea on my logic. Just adjust the width and the height of the include area and you should be fine.
    Code:
    <script language="JavaScript">
    <!--
    
    // 
    // Code Not Tested!!!
    // 
    
    var browser_name = navigator.appName;
    var include_file = null;
    var include_x    = 200;
    var include_y    = 500;
    
    if(browser_name == "Netscape") include_file = 'jdate.html';
    else if(browser_name == "Microsoft Internet Explorer") include_file = 'menu.html';
    
    document.write('<iframe src="'+ include_file +'" width="'+ include_x +'" height="'+ include_y +'">\n');
    
    //-->
    </script>
    Sleep is highly overrated.
  16. #9
  17. CSKA Sofia
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Location
    Germany / Bulgaria
    Posts
    575
    Rep Power
    123
    try this
    code taken from kingmike and modified

    Code:
    <script language="JavaScript">
    <!--
    
    // 
    // Code Not Tested!!!
    // 
    
    var browser_name = navigator.appName;
    var include_file = null;
    
    if(browser_name == "Netscape") include_file = 'jdate.html';
    else if(browser_name == "Microsoft Internet Explorer") include_file = 'menu.html';
    
    if(include_file != null){
    document.write('<iframe src="'+ include_file +'" width="200" height="500"></iframe>');
    }else{ document.write('<h1>GO HOME</h1>');
    }
    
    //-->
    </script>
    kingmike:
    iframe must also be closed
    Nikola Ivanov
    http://enikola.de
  18. #10
  19. Super Genius
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Orlando, Florida
    Posts
    314
    Rep Power
    13
    hehe - Thanks for the correction. Unfortunately, I can't blame a stupid mistake like fogetting to close an HTML tag on an inadequate knowlede of JavaScript. I'd rather say it was a rush job.

    Cheers,
    Mike
    Sleep is highly overrated.
  20. #11
  21. No Profile Picture
    Demonic Swordsman DGQB
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2003
    Posts
    1,043
    Rep Power
    83
    mike and execute: thanks, that wasn't a bad suggestion. I tried it, but the bgcolor of the iframe is white and doesn't match the yellowish color of the page, and I'm trying to avoid frames as much as possible

    On the other hand, I did find a page of help: http://www.devarticles.com/art/1/425/3

    From the source code, I have derived something like this:

    PHP Code:
    <?php 
    // 
    // SourceForge: Breaking Down the Barriers to Open Source Development 
    // Copyright 1999-2000 (c) The SourceForge Crew 
    // [url]http://sourceforge.net[/url] 
    // 
    // $Id: tim20000821.php3,v 1.2 2001/05/22 19:22:47 tim Exp $ 

    unset ($BROWSER_AGENT); 
    unset (
    $BROWSER_VER); 
    unset (
    $BROWSER_PLATFORM);

    function 
    browser_get_agent () 

      global 
    $BROWSER_AGENT
      return 
    $BROWSER_AGENT



    ?>

    <?php

      
    if (browser_get_agent()=='IE')
      { 
        include(
    "menu.html"); 
      }
      
      else if (
    browser_get_agent()=='MOZILLA'
      { 
        include(
    "jdate.html"); 
         }
    ?>
    Any suggestions, guys? The code doesn't quite work :P And please forgive me, as my knowledge of programming is basic.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Posts
    790
    Rep Power
    16
    This may sound like a stupid question but if you're using Javascript to detect the browser, why even bother trying to include a vbScript page for IE users?

    Just use Javascript for both browsers and be done with it.
  24. #13
  25. CSKA Sofia
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Location
    Germany / Bulgaria
    Posts
    575
    Rep Power
    123
    you dont you try with a separate script for deciding which page to be loaded and than just load the page?

    s.th. like this
    PHP Code:

    <?php

      
    if (browser_get_agent()=='IE')
      { 
        
    header('Location:IE.shtml');
      }
      
      else if (
    browser_get_agent()=='MOZILLA'
      { 
        
    header('Location:MOZILLA.shtml')
         }
      else{
        
    header('Location:YourDefaultSSIsite.shtml');
         }
    ?>
    Nikola Ivanov
    http://enikola.de

IMN logo majestic logo threadwatch logo seochat tools logo