When PHP files are used as JavaScript, Chrome interprets the contents of the file as HTML

Source: Internet
Author: User
Ads on our company's website dynamically generate JavaScript scripts in PHP and use PHP files directly as JavaScript files, usually on the home page with code like this

There's only one such sentence in this ad_js.php.
 
  


All other browsing did not appear to explain the file, except that Chrome interpreted it as HTML, meaning that there was only a bunch of comments left in the ad_js.php.

When I open Chrome's debugger a look that appears, such a warning:


Has anyone ever met a situation like this?
At present, my solution is directly echo that part of the JS code, but I want to maintain the original method.
Do you have a good way out?


Reply to discussion (solution)

You don't want to use it, but you still use echo.

Add type= "Text/javascript" Can you?

This PHP file needs to declare the type before output

Header (' Content-type:application/javascript ');

Otherwise, the default is declared as text/html, which naturally resolves to HTML format

As the source of the script tag, will it be interpreted as HTML?
The current situation is too bad, simple things have to be so complicated. I miss Microsoft's dominance of the world.
Style sheet requires text/css
JS needs Application/x-javascript

All returned files need to have a corresponding header description

As the source of the script tag, will it be interpreted as HTML?
The current situation is too bad, simple things have to be so complicated. I miss Microsoft's dominance of the world.
Style sheet requires text/css
JS needs Application/x-javascript

All returned files need to have a corresponding header description
It seems that it is not the problem, I can pass the test on other pages, I may be the JS code in a problem parsing problems

  • 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.