views:

58

answers:

1

Hey everyone, I am having a problem with a website I'm trying to build; basically I have a class that I call from my header file that loads all of the link and script tags. The link tags show up across all browsers, but the script tags only show up in safari and chrome, they do no show up in firefox or IE.

<script type='text/javascript' src='...'>  

now I have tried removing the "<" at the front of the tag just to see what happens, and it will show up as plain text then, but as soon as I put the "<" back it is again MIA.

So here is what's going on in the php. My header.php file calls the cms object function, located in cms.php, and those functions call other functions in my system.php file.

Now, again the link tags work w/out a hitch and I call them the exact same way ... it is just the dumb script tags. When I call the load_js("config"); function in my header.php it will load multiple tags as well. If it was just 1 tag I would put the script tags in the html rather than in the php, but I don't think I can do that when I'm producing multiple tags.

Any help would be great! Thanks in advance as well!

header.php

<?php echo $this->load_css("config"); ?>  
<?php echo $this->load_js("config"); ?> 

cms.php

function load_js($name){        
    // ...
    return header_script($name.".js");
    // ...
}
function load_css($name){       
    // ...
    return header_link($name.".css");
    // ...
} 

system.php

function header_script(){
    // 0 = src
    $num = func_num_args();
    if($num == 0){
        return;// if no arguments, can't successfully build header_script.
    }
    if($num == 1){
        return "<script type='application/javascript' src='".func_get_arg(0)."'></script>\n";
    }
}
function header_link(){
    $num = func_num_args();
    // 0 = rel
    // 1 = type
    // 2 = href
    if($num < 3){
        return; // can't successfully build link.
    }
    if($num == 3){
        return "<link rel='".func_get_arg(0)."' type ='".func_get_arg(1)."' href='".func_get_arg(2)."' />\n";
    }
}
+2  A: 

Firstly, application/javascript is not recognized by some browsers. You should change that to text/javascript

Secondly, as mentioned in comments (Justin Johnson), you should use parameters in your function definition with default values:

function header_script($name = '')
{
    if ($name != '') {
        return '<script type="text/javascript" src="'.$name.'"</script>';
    }
}
webbiedave
Single quotes around attribute values are, in fact, valid HTML, per the spec: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2
BoltClock
Thanks BoltClock. I've edited.
webbiedave
But will these suggestions solve the problem at hand?
Pekka
@Pekka: Will they not?
webbiedave
@webbiedave not if his problem is that the actual script tags do not appear in IE and FF. The OP is a bit hazy on this, though.
Pekka
@Pekka: That the OP did not mention that the PHP checked the agent string and behaved differently depending on that, I surmised that the same html is sent for each request, regardless of browser. Because of that, the bad `type` became a primary suspect.
webbiedave
when I said that the script tags don't show up I mean when you look at the source of the site they are completely missing. There are line breaks from the '\n' but the <script ...>...</script> tags are not there. Also when I remove the first "<" and only have "script ...>...</script>" then the tag will show up but now that it isn't recognized as a tag it is shown as plain text.
stmpy
I wasn't able to test this solution till today cause I don't have a windows computer at home, but I just tested it and it works, all I had to do was change the type to text/javascript! Thank you everyone for your suggestions, and for the advice.
stmpy