Why does php return a piece of js code?

Source: Internet
Author: User
Why does php return a js string? For example, echo & #039; & amp; lt; script & amp; gt; window. location. href & quot; index. phpAdminLogin & quot; & amp; lt; script & amp; gt; & #039; why does php return a js string? For example, echo 'script window. location. href = "/index. php/Admin/Login" script ';

Reply content:

Why does php return a js string? For example, echo 'script window. location. href = "/index. php/Admin/Login" script ';

JavaScript cannot be executed in php. js can only be executed after php is parsed into html. If you want to use php to output js code, you must use js code as a php string.

This should be to jump to the page. there is nothing strange about it. it depends on the specific requirements and application scenarios. For example, the following code

(In php) js is not running on the server, but on the client.

Php only returns the string "script" and "script" to the foreground without executing the statement. if you display the string in the browser, the browser automatically compiles and runs the script.

You can consider using file_get_contents ($ url );

It is usually determined in webpage requests that, if there is no login jump

If (! $ Login) {// It is no different from the output string, but the echo 'script "window will be executed by js. location. href = "/index. php/Admin/Login "script";}

The subject meant to ask, why didn't I skip?

I don't know what the subject means. I suggest you check it out.Question art...

Php can return any code, which is not discussed.

An intern previously asked me why PHP cannot throw exceptions to the browser. now, a sister colleague once tried to get a JS variable in php for judgment, the problem of the subject is basically not clear how a request is completed, and the program is executed in this way.

I try to explain: When PHP is used for development on the server side, PHP, HTML, JS, and CSS code is often written together, especially mixing PHP code in the last three (less CSS ). However, in actual execution, these parts are executed independently.

PHP can only be executed on the server. after the request is sent, the server executes the PHP script. for the PHP interpreter, it can only view and execute The code in the middle will generate the data required by the request after executing the code. sometimes the data is a json string, sometimes this is a page that contains HTML, JS, and CSS code.

The JavaScript code generated by the PHP interpreter is generated at this time. for the PHP interpreter, the JS code is no different from the common string.

After that, the server reads the data from the PHP buffer (for example, a page containing HTML, JS, and CSS code) and then packs the data and sends it to the browser. The browser is embedded with interpretation programs for HTML, JS, and CSS, so the browser can execute HTML, JS, and CSS code. Similarly, because the browser does not have a PHP interpreter, the browser cannot execute PHP code. The subject can try to write a static page locally, write a piece of PHP code in it, and then use a browser to open and observe.

To put it simply, there is such a wall. one side of the wall is a browser, which can execute HTML, JS, and CSS. the other side of the wall is a server, which can execute PHP; the two interact through the HTTP protocol. For the browser, because it does not know PHP code, even if you give it a piece of PHP code, it is also a bunch of common strings. Similarly, for servers, the so-called HTML, JS, and CSS code are no different from "Hello, world.

Both JS and PHP can generate any form of strings, so they can generate code in any language.
In other words, if there is no program to explain the execution, any code is just a bunch of strings.

Some of my personal opinions are welcome to correct me.

For the answer, see @.

In the era of asp.net webform, there were "server-side registration scripts to the client" operations. after all, the front-end has not yet emerged.

You should split this question into two questions:

  1. Why does php return a piece of html code?

  2. Why does html contain a piece of js code?

Big mistake ~

Jsonp, a programming method that supports cross-origin.

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.