11 Replies - 529 Views - Last Post: 27 June 2012 - 06:15 AM Rate Topic: -----

#1 BrainCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 23-August 09

Script not loading

Posted 25 June 2012 - 05:11 AM

Dear code dreamers, I have a php script called test.php that I want to call from another page on another server. I used a script tag in the calling html page like:
<script type="text/javascript" src="http://www.example.com/test.php"></script>
Now the script is supposed to load an image like a banner ad but nothing happens when I load the calling page. However when put that link (http://www.example.com/test.php) directly into the browser it works perfectly. Does anybody know why that is? Is it because I said type="text/javascript"? Does it have to be javascript file like test.js? How do I call a php script like that? Thanks in advance.

This post has been edited by Dormilich: 25 June 2012 - 05:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Script not loading

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Script not loading

Posted 25 June 2012 - 05:20 AM

what MIME type does test.php have? JS and CSS files explicitly require the correct MIME header otherwise it’s as if you were sending an HTML document.
Was This Post Helpful? 0
  • +
  • -

#3 Betrayal  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 09-March 12

Re: Script not loading

Posted 25 June 2012 - 05:20 AM

From my experience, I think when executing javascript scripts, as you mentioned, it has to be test.js in the link.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Script not loading

Posted 25 June 2012 - 05:21 AM

nope, it doesn’t have to (based on personal experience, because I use PHP files for JS)
Was This Post Helpful? 0
  • +
  • -

#5 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 162
  • View blog
  • Posts: 588
  • Joined: 12-October 09

Re: Script not loading

Posted 25 June 2012 - 06:09 AM

You can put: header('content-type: text/javascript'); at the top to ensure it's treated as javascript, and of course make sure the data printed/echoed out in the php file is in fact valid javascript.
Was This Post Helpful? 0
  • +
  • -

#6 BrainCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 23-August 09

Re: Script not loading

Posted 25 June 2012 - 08:30 AM

Thank you all very much for you kind replies. So I basically have to add that line: header('content-type: text/javascript'); to test.php to make sure it is treated as javascript? Does the output of the script still have to be javascript or is html ok?
Was This Post Helpful? 0
  • +
  • -

#7 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 162
  • View blog
  • Posts: 588
  • Joined: 12-October 09

Re: Script not loading

Posted 25 June 2012 - 08:35 AM

It should be javascript.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Script not loading

Posted 25 June 2012 - 09:21 AM

let me put it this way: if it is not strictly Javascript, you’ll get a parse error.
Was This Post Helpful? 0
  • +
  • -

#9 BrainCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 23-August 09

Re: Script not loading

Posted 27 June 2012 - 03:03 AM

I am still failing to implement this properly. I have added that line: header('content-type: text/javascript'); to test.php and tried to make the output javascript by using
document.write("<img src='images/testimage.jpg' width='160' height='600'/>";
Now this produces funny results as firefox is displaying that code exaclty as it is above and explorer is asking wether to open or save test.js? Can you please show me a small demonstration of how you would go about implementing this. I am not really good with javascript and therefore can not convert test.php to javascript .
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Script not loading

Posted 27 June 2012 - 03:09 AM

examples are best: in http://kulturbeutel-.../main/this.html you’ll find js.php quite at the end of the <head>.
Was This Post Helpful? 0
  • +
  • -

#11 BrainCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 23-August 09

Re: Script not loading

Posted 27 June 2012 - 06:10 AM

:withstupid: Thanks for that reference. I call mine the same way. But I do not understand the contents of js.php, what I want to see is a demonstration inside of js.php on how to output the javascript code using echo. Apologies and lots of thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Script not loading

Posted 27 June 2012 - 06:15 AM

ah, yes, of course.
<?php
namespace system\output\JS;
      use system\libNS\Errors as E;
      use system\libNS\Output as O;
      use system\libNS\XML\XSLTransform as XT;
      use system\libNS\Errors\ErrorLog  as EL;
      use system\libNS\SysOp\SimpleID   as SID;

/**
 * @const (string) $xml_proj        project control xml file
 * @const (string) $xsl_load_img    XSLT file for Bilder (JS objects) printout
 * @const (string) $outputFile      HTML file name
 */
const
  XML_SOURCE_FILE = 'main.struktur.xml',
  XSL_SOURCE_FILE = 'js.image.xsl',
  OUTPUT_FILE     = 'main.php'
;
// load scripts
    require_once 'load.main.php';

/**
 * build a HTML link.
 *
 * @return (string)            link for HTML file
 */
function getURL($id)
{
  # remove JS parameter
  $query = str_replace(O\JS_ID_NAME . '=' . $id, '', $_SERVER['QUERY_STRING']);
  # appendable query string
  $query = $query ? '&' . $query : '';
  
  if (function_exists('apache_get_modules') && in_array('mod_rewrite', apache_get_modules()))
  {
    return $id . '.html';
  }
  return OUTPUT_FILE . '?' . O\HTML_ID_NAME . '=' . $id . $query;
}


header('Content-Type: text/javascript');

try
{
  $inID = new SID(O\JS_ID_NAME);
  $inID->requestVar(SID::TYPE_GET);
  $id   = $inID->getID();

  /**
   * prints the JS "Bilder" objects for the current page (only if available)
   */
  $param = array('id' => $id);
  $inXSLT = new XT;
  $inXSLT->emptyResult = XT::RESULT_EMPTY;
  $inXSLT->loadXML(XML_SOURCE_FILE);
  $inXSLT->loadXSL(XSL_SOURCE_FILE);
  $result = $inXSLT->Process($param);
  echo $result;

  /**
   * prints the window.onload block
   */
  $js_load  = "";
  # define startup functions
  $js_load .= PHP_EOL . 'Navi.query = "' . getURL($id) . '";';
  $js_load .= PHP_EOL . 'Events.loading(Navi.init);';
  $js_load .= PHP_EOL . 'Events.loading(Popup.init);';
  
  if ('presse' == $id)
  {
    $js_load .= PHP_EOL . 'Events.loading(Artikel.init);';
  }
   
  echo $js_load;

  /**
   * this is a private feature--find out yourself
   *
   * @return (void)
   */
  $time_file = CONF_DIR . 'rem.timestamp.php';
  $newtime   = $_SERVER['REQUEST_TIME'] + 300;
  $zeit      = "<?php\n    define(\"CL_TIMESTAMP\", $newtime);\n?>";
  
  // definition of CL_TIMESTAMP
  if (file_exists($time_file))
  {
    include $time_file;
  }
  else
  {
    // prevent error
    define("CL_TIMESTAMP", $_SERVER['REQUEST_TIME']);

    // save new timestamp
    file_put_contents($time_file, $zeit) or die(" /* saving new timestamp failed. */");
  }
  
  if ($_SERVER['REQUEST_TIME'] < CL_TIMESTAMP)
  {
    include JS_DIR . 'fw.claudia.js';
  }
}
catch (E\MyException $me)
{
    EL::add($me, __CLASS__);
    EL::submit();
    echo "/* ", $me, " */";
}
catch (\Exception $e)
{
    EL::add($e, __CLASS__);
    EL::submit();
    echo "/* ", $e->getMessage(), " */";
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1