When php data is used as javascript, chrome interprets the file content as html

Source: Internet
Author: User
When php is used as javascript, chrome interprets the file content as html. our website advertisement uses php to dynamically generate javascript scripts, and directly uses the php file as a javascript file, on the home page, there is usually such code & lt; script & nbsp; src = 'XXX/ad_j. When PHP is used as javascript, chrome interprets the file content as html.
The advertisement on our company's website uses php to dynamically generate javascript scripts and directly use the php file as a javascript file. such code is usually found on the home page.
 
  《script》
 

This ad_js.php only has such a statement.




No exception occurred when explaining the file for all other browsing operations, but chrome interpreted it as html. that is to say, only a bunch of comments are left in ad_js.php.

When I open the chrome debugger, the following warning appears:


Has anyone ever encountered such a situation?
Currently, my solution is to directly echo the js code, but I want to maintain the original method.
Do you have a good solution?
------ Solution --------------------
Although you don't want to use it, you still need to use echo.
------ Solution --------------------
Can I add type = "text/javascript "?
------ Solution --------------------
The PHP file must declare the type before output.
header('Content-Type: application/javascript');

Otherwise, it is declared as text/html by default, and should be parsed as html format
------ Solution --------------------
As the script tag source, it must be interpreted as html?
The current world is too bad, and simple things must be so complicated. Miss the world dominated by Microsoft
Style sheets require text/css
Js requires application/x-javascript

All returned files must have corresponding header descriptions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.