2

I have this script below and its working good. It works when I load the .php file directly even with external javascript tags.

index.html

<div id="content"></div>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="test.js"></script>

test.js

$(document).ready(function() {
    $('#content').load('content/index.php')
});

/content/index.php

<?php
    require_once("geoip.inc");
    $gi = geoip_open("GeoIP.dat", GEOIP_STANDARD);
    $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);

    if ($country_code == 'RO' ||
        $country_code == 'DO' ||
        $country_code == 'PK' ||
        $country_code == 'MA' ||
        $country_code == 'PE' ||
        $country_code == 'IR' ||
        $country_code == 'DZ' ||
        $country_code == 'RU' ||
        $country_code == 'EG') {
        echo 'country not supported';
    } else {
        echo 'supported';
    }
?>

My Question is:

When I am trying to

echo 'supported';

It is working good.

But when I tried to echo

<SCRIPT SRC="http://externalsite.com/ads/sample.js" TYPE="text/javascript"></SCRIPT>

the script tag wasn't show. I need to load the .php file inside my .js file

Community
  • 1
  • 1
Rai Jones
  • 21
  • 3

3 Answers3

0

try this..

echo "<SCRIPT SRC='http://externalsite.com/ads/sample.js' TYPE='text/javascript'></SCRIPT>";
Ajay Makwana
  • 2,330
  • 1
  • 17
  • 32
0

When you try like below:

<?php
echo 'test';
echo '<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>';
die;
?>

Output will be:

test

Source code will be look like

test<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

UPDATE

You are loading an entire HTML page into your div, including the html, head and body tags. What happens if you do the load and just have the opening script, closing script, and JavaScript code in the HTML that you load?

Here is the driver page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>jQuery Load of Script</title>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
            google.load("jquery", "1.3.2");
        </script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#myButton").click(function() {
                    $("#myDiv").load("trackingCode.html");
                });
             });
         </script>
    </head>
    <body>
        <button id="myButton">Click Me</button>
        <div id="myDiv"></div>
    </body>
</html>

Here is the contents of trackingCode.html:

<script type="text/javascript">
    alert("Outside the jQuery ready");
    $(function() {
        alert("Inside the jQuery ready");
    });
 </script>

This works for me in Safari 4.

Update: Added DOCTYPE and html namespace to match the code on my test environment. Tested with Firefox 3.6.13 and example code works.

For more refer this link

Community
  • 1
  • 1
Kausha Mehta
  • 2,828
  • 2
  • 20
  • 31
  • Actually, my PHP file works, its just I got a problem when I call it on my .html file. – Rai Jones Jan 12 '16 at 06:23
  • My PHP file works even I add the script tags to echo. My problem is when I tried to load it in jquery, the script tags wont show. But it can echo the 'not supported' word. I think it cant read the script tags when fetching by test.js? – Rai Jones Jan 12 '16 at 06:30
  • @RaiJones Yes, it can't..... So you can copy that script and put it to the top of filewhere you include all other script..... Thats it! – Kausha Mehta Jan 12 '16 at 06:35
  • @RaiJones I'm going to update my answer please check that. – Kausha Mehta Jan 12 '16 at 06:41
  • Thanks Kausha, will try it and will let you know with the outcome :) – Rai Jones Jan 12 '16 at 06:57
0

Echo tags will not be displayed on page but contains in HTML. see if script tag present in HTML using browser inspector.

If you want to display tags on page then use htmlspecialchars()

echo htmlspecialchars("<SCRIPT SRC='http://externalsite.com/ads/sample.js' TYPE='text/javascript'></SCRIPT>");

Refer: http://php.net/manual/en/function.htmlspecialchars.php

devpro
  • 16,184
  • 3
  • 27
  • 38
Vegeta
  • 1,319
  • 7
  • 17