0

The description of the title may not be clear. Or Yii2 solve this problem is not the idea. The specific needs are like this - Other sites need to reference a page of this site developed by Yii. This page needs to output the standard js. The output code example is as follows -

document.write('<a href="https://test.com/counter.php">test string</a>');

Other sites are quoted in the following ways -

<script type="text/javascript" src="https://test.com/counter.php"></script>
RedLuo
  • 1
  • 3
  • I think that your problem is not related with **yii2**. You always can execute javascript in any place that you want. And also write links to other code sources as in your example. – Juan Antonio Sep 10 '19 at 11:47
  • This is not a Yii2 (Yii) question ... your question is not clear – ScaisEdge Sep 10 '19 at 14:36
  • not clear what you are asking. but `document.write` [should be avoided](https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) – Muhammad Omer Aslam Sep 10 '19 at 15:00

1 Answers1

0

My solution

Yii::$app->response->format = Response::FORMAT_RAW; 
Yii::$app->response->headers->set('Content-Type', 'application/javascript');
Yii::$app->response->data = "document.write('<a href=\"https://test.com/test.php\">test string</a>');";
return Yii::$app->response->send();
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
RedLuo
  • 1
  • 3