Solution of javascript code can not be executed in ThinkPHP controller

  • 2021-08-05 09:26:41
  • OfStack

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

Here example analysis under 1 thinkphp in the controller page effect code can not execute the solution, take "exit" this item, my "exit system" link is written to the left frame, with js dynamically generated. That is, it cannot be specified by target in the link.

$this->assign('jumpurl',__url__.'/login');<br>$this->success(" Logout successful! ");

If you write this way, the page to jump to will be displayed in the right frame. I can't write js code into url. I commented out these two lines of code. Use directly:
echo "<script>window.top.location.href='$url';</script>";

This is not good either. On the one hand, url can't use the constants __app__, and on the other hand, the result of the final execution is actually < script > The label is removed and the rest is output directly!
(window.top.location.href='index/login';)

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

Then I suddenly thought of a problem. The labels in tp are all in angle brackets ( < > ), which I wrote in the controller < script > Is it parsed as a label? So I rewrote the second code:

echo '<literal><script>alert(" Successful exit ");top.location.href="login";</script></literal>';

That's it.

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


Related articles: