Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

Treat PHP file as script

php js html javascript cross-compatibility website webpage frankenstein

Best Answer Vaielab, 07 January 2014 - 09:56 AM

This should work... you can always try to force the mime type with this command:

header('Content-Type: text/javascript');
Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1 mailMindlin

mailMindlin

    CC Regular

  • Member
  • PipPipPip
  • 27 posts

Posted 07 January 2014 - 08:52 AM

On my website, I have a PHP script that prints javascript code, such as 

function baz(){
alert("$user");
}
baz();

called foo.php.

In the html, I have

<script type="text/javascript" src="foo.php"></script>

The webbrowser downloads foo.php, but no alert is showing up, and I can't run baz() from the console.

In google chrome, the console says

Resource interpreted as Script but transferred with MIME type text/html: "http://www.example.com/foo.php"

How can I get it to run as a script?

Edit: BTW, the script is custom generated per user, and is sent over HTTPS, so I can't use AJAX to get $user


Edited by mailMindlin, 07 January 2014 - 09:01 AM.


#2 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 07 January 2014 - 09:35 AM

In your php script, do you have a php tag open like so

<?php
function baz(){
alert("$user");
}
baz();
?>

Or it is in simple text like so

function baz(){
alert("$user");
}
baz();

?


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#3 mailMindlin

mailMindlin

    CC Regular

  • Member
  • PipPipPip
  • 27 posts

Posted 07 January 2014 - 09:53 AM

It's more like

<?php
//foo.php
[code]...
echo 'function baz(){
alert("$user");
}';
?>
baz();


#4 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 07 January 2014 - 09:56 AM   Best Answer

This should work... you can always try to force the mime type with this command:

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

You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#5 mailMindlin

mailMindlin

    CC Regular

  • Member
  • PipPipPip
  • 27 posts

Posted 07 January 2014 - 10:02 AM

[DELETED]


Edited by mailMindlin, 07 January 2014 - 11:04 AM.


#6 Poe

Poe

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 81 posts

Posted 18 March 2014 - 11:57 AM

actually, the proper header is this:

<?php header("Content-type: application-x/javascript"); ?>

 

as there is no type text/javascript so it will just return a .txt file not a .js

 

js is an application script, not text.


"Portability is for those who can't write new programs" - Linus Torvalds


#7 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 19 March 2014 - 08:33 AM

actually, the proper header is this:

<?php header("Content-type: application-x/javascript"); ?>

 

as there is no type text/javascript so it will just return a .txt file not a .js

 

js is an application script, not text.

 

 

 

text/javascript (Obsolete): JavaScript; Defined in and made obsolete in RFC 4329 in order to discourage its usage in favor of application/javascript. However, text/javascript is allowed in HTML 4 and 5 and, unlike application/javascript, has cross-browser support. The "type" attribute of the <script> tag in HTML5 is optional and there is no need to use it at all since all browsers have always assumed the correct default (even in HTML 4 where it was required by the specification).

application-x/javascript or application/javascript does not have cross-browser support, but text/javascript does


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download