tags:

views:

106

answers:

2

I have a PHP script that publishes a customised javascript based on the parameter, with header type as text/javascript. This PHP url is included in src of a script tag. However, there seem to be some issue. That script seems to be non functional.As in, I have a alert inside that, which should be executed immediately after inclusion. Its not happening . Where am I going wrong? Thanks in advance

Server Side PHP

<?php
//Exploding the path after the file widget to get user details
$expl = explode("/",$_SERVER['PATH_INFO']);
$c=count($expl);

//Handling the cases as widget/a widget//a etc 
switch($c) {
    case 2:
     if(empty($expl[0]) && !(empty($expl[1]))) pumpValid();
     else pumpInvalid();
     break;
    case 3:
     if(empty($expl[2]) && !(empty($expl[1])) && empty($expl[0])) pumpValid();
     else pumpInvalid();
     break;
    default:
     pumpInvalid();
     break;
}


function pumpValid() {
    global $expl;
    //Checking for a matching account in the urllist
    include('embedUrl/urllist.php');
    if(isset($customerList[$expl[1]])) {
     header("Content-Type: text/javascript");
     //Setting the host path  for fetching the JS files later. As in stage or vidteq.com
     echo "alert('h');";
     echo 'var _serverHostUrl="http://'.$_SERVER["SERVER_NAME"].eregi_replace('widget.*','',$_SERVER["REQUEST_URI"]).'";';
    }
    else
     pumpInvalid();
}

function pumpInvalid() {
    //Should redirect to error/home page
    echo "Are You Nuts";
}
?>

function init() {
    alert('hi'); 
    addJSinHead('jquery-1.3.2.min.js');
    addJSinHead('OpenLayers.js');
    addJSinHead('json2.js');
    addJSinHead('dom-drag.js');
    addJSinHead('globals.js');
}

function addJSinHead(fileName) {
    var head=document.getElementsByTagName('head');
    var new=document.createElement('scrpit');
    new.src=_serverHostUrl+'/js'+fileName;
    new.type='text/javascript';
    head.appendChild(new);
}

init();

Inclusion in client side HTML

<script src='http://rak/cvs/widget/cis/' type='text/javascript'></script>
+1  A: 

Is the alert that should be executed inside of a function block? If so then you first need to execute the actual function.

Also try copying and pasting the javascript src url directly into the browser's url bar.

If the above didn't help, some code to analyze would be useful.

klaaspieter
The alert is outside the function , so should be executed immediately. I have attached the code in question
Rakesh
A: 

You can check with Firebug to see if the output of the script is what you expect and that the headers are being sent as you expect.

You'll need to provide some code for further help.

EDIT: I don't see anywhere that you output a header in php:

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

EDIT2: It looks like you're calling the pumpValid function before it's defined.

smack0007
Hi, its there in pumpValid function. I believe I got the reason. Do I need to echo each line of Javascript? Leaving it outside PHP tags won't work?
Rakesh