SoFunction
Updated on 2025-04-04

Solutions to the javascript code not being executed in ThinkPHP controller

This article describes the solution to the inability to execute javascript code in ThinkPHP controller. Share it for your reference. The specific methods are as follows:

Here, let’s analyze the solution to the problem that thinkphp’s web page special effect code cannot be executed in the controller. Take the “exit” item as an example. My “exit system” link is written in the framework on the left and is dynamically generated using js. That is to say, it cannot be specified through the target in the link.

Copy the codeThe code is as follows:
$this->assign('jumpurl',__url__.'/login');<br>$this->success("Logout successful!");

If you write this way, the page you want to jump will be displayed in the right frame. It is not possible to write the js code into the url. I commented out these two lines of code. Use directly:
Copy the codeThe code is as follows:
echo "<script>='$url';</script>";

This is not possible. On the one hand, the constants such as __app__ cannot be used in the url. On the other hand, the final result is that the <script> tag is removed and the remaining direct output is!
Copy the codeThe code is as follows:
(='index/login';)

I checked the success function in the action class and found no relevant parameters.

Later, I suddenly thought of a question. The tags in tp are all included in angle brackets (<>). Is the <script> I wrote in the controller parsed as a tag? So I rewrite the second code:

Copy the codeThe code is as follows:
echo '<literal><script>alert("Exit Successfully");="login";</script></literal>';

That's all.

I hope this article will be helpful to everyone's ThinkPHP framework programming.