SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "Hidding" javascript when it's included in php?

    Not sure where to post this, but ill try here.

    When I add/include an clean javascript like this:
    PHP Code:
    echo '<script type="text/javascript" src="js/ajax.js"></script>'
    It looks nice and needy in my source, but sometimes I need to put some PHP inside the javascript and I then need to make my javescript PHP and include the script like this:
    PHP Code:
    include_once("js/ajax.php"); 
    This makes it viewable in my source and doesnt look nice... Is there another way to do this so the whole PHP javascript doesnt get into my source?

    Thanks in advance...

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If your setting custom vars in your javascript source that need to be set with PHP do so like this for example...

    PHP Code:
    echo '<script type="text/javascript">
        var myVar = "'
    .$something.'";
        var new   = "'
    .$newThing.'";
    </script>'
    ;
    echo 
    '<script type="text/javascript" src="js/ajax.js"></script>'
    This is how i do it when i need JS vars declared with PHP values and it for me personally is a lot easier then managing PHP files for JS.

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SgtLegend View Post
    If your setting custom vars in your javascript source that need to be set with PHP do so like this for example...

    PHP Code:
    echo '<script type="text/javascript">
        var myVar = "'
    .$something.'";
        var new   = "'
    .$newThing.'";
    </script>'
    ;
    echo 
    '<script type="text/javascript" src="js/ajax.js"></script>'
    This is how i do it when i need JS vars declared with PHP values and it for me personally is a lot easier then managing PHP files for JS.
    But how do I then get the vars into ajax.js?

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Declaring the vars before the JS file makes them part of the global scope there for in your ajax.js you can do something like

    Code JavaScript:
    function ajaxConnection(){
        var connect_to = myVar;
        var newSet     = new;
    }

    NOTE: This is just an example, the method new is a reserved word and cannot be used.

  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't get it to work...
    Code:
    <script type="text/javascript">
    	var nlat = "48";
    	var nlng = "53";
    	var nzoom = "121";
    </script>
    Are then trying to get it like this:
    Code JavaScript:
    function load() {
       var nlat = nlat;
       var nlng = nlng;
       var nzoom = nzoom;
    }

    But with no luck... The vars just says "nlat,nlng,nzoom"...

    Where do I go wrong?

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try using var names in the load() function besides the global scope vars

  7. #7
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nomatter how I do this it won't work... In my source it only shows up as nlat, nlng, nzoom... I want it to show the numbers...

    Here is the script...
    Code JavaScript:
    var nlat = 56.26392;
    var nlng = 10.501785;
    var nzoom = 6;
     
    function load(nlat,nlng,nzoom) {
      if (GBrowserIsCompatible()) {
    	var map = new GMap2(document.getElementById("map")); 
    	map.setMapType(G_HYBRID_MAP);
    	map.addControl(new GSmallMapControl());
    	map.setCenter(new GLatLng(nlat, nlng), nzoom);
     
    	var zoomlevel = map.getZoom();
      }
    }

    What to do...

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Call load() when an event such as page load is triggered.

    <body onload = "load(nlat, nlng, nzoom );">

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,799
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Why not use:

    Code:
    echo '<script type="text/javascript" src="js/ajax.php"></script>';
    That way the file containiing the JavaScript gets processed for any PHP it contains on the server before it is passed to the browser for processing as JavaScript.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  10. #10
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,026
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Why not use:

    Code:
    echo '<script type="text/javascript" src="js/ajax.php"></script>';
    That way the file containiing the JavaScript gets processed for any PHP it contains on the server before it is passed to the browser for processing as JavaScript.
    Browser caching. Even if no-cache headers are sent, even with 'must-revalidate' flags in place I wouldn't be surprised to find at least a few browsers ignoring them for the life of the session because this is a support file and not the main page file. I could be wrong but I'd test for the possibility.

    My suggestion is to use a X-JSON header to hold the values. Those aren't just for ajax responses - they can be embedded into a page feed and read by the javascript of the page.

  11. #11
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,799
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Browser caching.
    I have never had that problem but then the PHP in the file should only need to be run the once prior to the page first loading. If you want to run more PHP after the page loads then an ajax call from within the JavaScript is appropriate.

    The only difference between having the JavaScript embedded inside the HTML of a PHP page and what I suggested is that one is jumpled inside the HTML and the other is not. In both cases all the PHP runs before the files are sent to the browser.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •